Store tokens after splitting
I have the following Perl statement that splits a string into delimiters |, \ or /
@example = split(/[\|\\\/]/,$i);
How to save tokens after splitting?
For example, entering:
John | Mary / Matthew
I get:
(John, Mary, Matthew)
What I want:
(John, |, Mary, /, Matthew)
+3
Nissa
source
to share
1 answer
Place the capturing group inside your regex to keep the delimiter:
my $str = 'John|Mary/Matthew';
my @example = split /([\|\\\/])/, $str;
use Data::Dump;
dd @example;
Outputs:
("John", "|", "Mary", "/", "Matthew")
This is documented in the last paragraph: http://perldoc.perl.org/functions/split.html
+10
Miller
source
to share