Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Regex to get everything before first /

by ikegami (Pope)
on Jun 06, 2011 at 19:44 UTC ( #908357=note: print w/replies, xml ) Need Help??


in reply to Regex to get everything before first /

my ($stuff) = $thing =~ m{^([^/]*)};
my ($stuff) = $thing =~ m{^(.*?)/}s;
my ($stuff) = split(qr{/}, $thing, 2);
my ($stuff) = split(qr{/}, $thing);

The second requires the presence of a "/", and uses the non-greedy modifier which I prefer to avoid.

Replies are listed 'Best First'.
Re^2: Regex to get everything before first /
by FunkyMonk (Chancellor) on Jun 06, 2011 at 21:11 UTC
    the non-greedy modifier which I prefer to avoid
    For what reason(s)?

      It fails poorly when there's a problem because the /.*?/ in /.*?// can match "/". It's only indirectly through co-operation with the rest of the pattern that it might be prevented from doing so. I prefer being more direct and explicitly prevent what should not be matched from matching.

Re^2: Regex to get everything before first /
by jwkrahn (Monsignor) on Jun 06, 2011 at 22:05 UTC

    Another one:

    my ($stuff) = $thing =~ m{[^/]*}g;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2021-01-22 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?