Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: split with separators but dont include in the array

by anniyan (Monk)
on Oct 17, 2005 at 15:44 UTC ( #500751=note: print w/ replies, xml ) Need Help??


in reply to Re: split with separators but dont include in the array
in thread split with separators but dont include in the array

Roy Johnson and ikegami thanks for your quick replies. Here i have one question. First i tried like the following.

@arr = split /(&&|\|\|)/, $str;

But it included && and || in the array. Then only i went for lookahead option.

But as per yours reply it works fine if i remove that extra grouping.

@arr = split /&&|\|\|/, $str;

Why is it so? Could you please explain. Then if there is many separators how to with that?

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)


Comment on Re^2: split with separators but dont include in the array
Select or Download Code
Re^3: split with separators but dont include in the array
by Roy Johnson (Monsignor) on Oct 17, 2005 at 15:55 UTC
    Anything in capturing (plain) parentheses will be returned as a separate item in the split results. If you need parentheses for grouping, use non-capturing parentheses, which open with (?: instead of just (.

    No matter how many alternative separators you have, you don't need parentheses at all unless the alternatives are only part of the separator and you need to "factor out" common substrings. For example, if your valid separators were "foo&&", "foo||", and "++", you could split on

    /foo(?:&&|\|\|)|++/

    Caution: Contents may have been coded under pressure.
Re^3: split with separators but dont include in the array
by McDarren (Abbot) on Oct 17, 2005 at 15:57 UTC
    @arr = split /(&&|\|\|)/, $str;
    But it included && and || in the array.

    That's because you used capturing parentheses.

    Then if there is many separators how to with that?

    You use the pipe "|" character to separate multiple options, like so:
    @arr = split /foo|bar|meh/, $str;
    That would split the string on any occurrences of "foo", "bar" or "meh" without including them in the resultant list.
    This may not be clear in your original example, as one of the strings you are splitting on is a double-pipe "||", so the escaping of those adds a bit of "noise" to the split expression :)
Re^3: split with separators but dont include in the array
by polettix (Vicar) on Oct 17, 2005 at 23:33 UTC
    The manual (split in this case) can usually dissipate such doubts:
    If the PATTERN contains parentheses, additional list elements are created from each matching sub- string in the delimiter. + split(/([,-])/, "1-10,20", 3); + produces the list value + (1, '-', 10, ',', 20)

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2014-11-27 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (183 votes), past polls