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

a regex to split this...

by s_m_b (Acolyte)
on Aug 09, 2006 at 07:38 UTC ( [id://566382]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

s_m_b has asked for the wisdom of the Perl Monks concerning the following question:

should be simple, but I just cannot get it working:
domain\userid
(or)
domain/userid
from $ENV{'REMOTE_USER'}

I've tried using both
undef, $userid = split('/', $ENV{'REMOTE_USER'};
(and)
$username =~ /(cyc(\|\/)(.*?)/;
- 'cyc' is the domain string btw.
I get either nothing back from $1, $2 etc, or various errors relating to the regex upsetting the split.

Replies are listed 'Best First'.
Re: a regex to split this...
by imp (Priest) on Aug 09, 2006 at 08:00 UTC
    You need to use the non-capturing pattern (?:foo) for the alternation unless you want it to be included in the results.
    (undef, $userid) = split(qr{(?:/|\\)}, $ENV{'REMOTE_USER'});
    Update
    You actually don't need the grouping at all, and could just use alternation without parens, or could use a character class. These all work the same:
    # Alternation with non-capturing clustering (?:foo) (undef, $userid) = split(qr{(?:/|\\)}, $ENV{'REMOTE_USER'}); # Alternation without clustering (undef, $userid) = split(qr{\\|/}, $ENV{'REMOTE_USER'}); # Character class including '\' and '/' (undef, $userid) = split(qr{[\\/]}, $ENV{'REMOTE_USER'});

      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.
        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
Re: a regex to split this...
by borisz (Canon) on Aug 09, 2006 at 08:02 UTC
    ( undef, $userid ) = split /[\\\/]/, $ENV{'REMOTE_USER'};
    Boris
Re: a regex to split this...
by Leviathan (Scribe) on Aug 09, 2006 at 08:05 UTC

    your split works in a scalar context, so you need:

    (undef, $userid) = split "/", ....

    Which doesn't match both cases, so you need:

    (undef, $userid) = split "/|\\\\", $ENV{REMOTE_USER};

    Yes, you need 4 slashes.

    Also in your regex you need to escacpe the backslash:

    $username =~ m#(cyc)/|\\(.*)#;
    --
    Leviathan.
Re: a regex to split this...
by s_m_b (Acolyte) on Aug 09, 2006 at 10:05 UTC
    thanks for the replies - a lot of little subtle bits there, but its working now!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://566382]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.