Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Splitting inside an array

by kilinrax (Deacon)
on Feb 05, 2001 at 22:10 UTC ( #56457=note: print w/replies, xml ) Need Help??


in reply to Splitting inside an array

This looks like a job for zero-width lookahead and lookbehind assertions:
#!/usr/bin/perl -w use strict; my $str = "x:= y + z;"; my @tokens = split /\s+|(?<=\w)(?=(?:;|:=))/, $str; print join "\n", @tokens;
I don't know if you find commented regular expressions helpful, but just in case you do:
my @tokens = split / \s+ # whitespace | # or (?<=\w) # after a word character (?= # before a ..... (?: # non-backreferencing paranthesis (to pre +vent adding matches to list returned) ; # a semicolon | # or := # colon equals; add more operators to spl +it on after here ) ) /x, $str;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://56457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-21 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?