Reputation: 215
I have the the following Perl statement which split a string by delimiters |,\ or /
@example = split(/[\|\\\/]/,$i);
How to store the tokens after splitting?
For example input:
John|Mary/Matthew
What I get is:
(John, Mary, Matthew)
What I want:
(John, |, Mary, /, Matthew)
Upvotes: 3
Views: 86
Reputation: 35208
Put a capturing group inside your regex to save 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 of: http://perldoc.perl.org/functions/split.html
Upvotes: 10