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 (Bishop) 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 (Abbot) on Jun 06, 2011 at 22:05 UTC

    Another one:

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