in reply to Re: a regex to split this...
in thread a regex to split this...

You don't need to do that, because the delimiter is included in the result if the pattern has parenthese.

print join ":", split "-", "1-2-3-4"; # as opposed to: print join ":", split "(-)", "1-2-3-4";
--
Leviathan.

Replies are listed 'Best First'.
Re^3: a regex to split this...
by imp (Priest) on Aug 09, 2006 at 12:42 UTC
    I initially reacted to this line in the OP:
    $username =~ /(cyc(\|\/)(.*?)/;
    Which had several problems.
    • Does not assign the resulting match
    • Should use non-capturing clustering or a character class for the '/' or '\'
    • Has an extra opening paren
    • Uses non-greedy match to select the username
    These would work:
    ($username) = $ENV{REMOTE_USER} =~ /cyc(?:\\|\/)(.+)/; ($username) = $ENV{REMOTE_USER} =~ /cyc[\/\\](.+)/; # And to cleanup a little ($username) = $ENV{REMOTE_USER} =~ m{cyc(?:\\|/)(.+)}; ($username) = $ENV{REMOTE_USER} =~ m{cyc[/\\](.+)};
    But not having coffee yet I corrected the split version instead.

    It's also worth nothing that split("\\|/","a|/b") does not work as one might think.

    perl -e 'my ($a,$b) = split("\\|/","a/b"); print "a:$a\nb:$b\n"' a:a/b b:
    This happens because that pattern is actually looking for a literal '|/', as shown here:
    perl -MO=Deparse -e 'my ($a,$b) = split("\\|/","a/b");' my($a, $b) = split(m[\|/], 'a/b', 3); perl -e 'my ($a,$b) = split("\\|/","a|/b"); print "a:$a\nb:$b\n"' a:a b:b