Nissa
Nissa

Reputation: 215

Store the tokens after splitting

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

Answers (1)

Miller
Miller

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

Related Questions