Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store the tokens after splitting

Tags:

split

token

perl

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)

like image 934
Nissa Avatar asked Dec 08 '25 18:12

Nissa


1 Answers

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

like image 140
Miller Avatar answered Dec 11 '25 13:12

Miller