in reply to Regex to get everything before first /
toolic hit the nail on the head, but you might choose a different delimiter so that it wouldn't be necessary to escape the forward slash, like this:
my $thing = 'foo/bar/goo'; ( my $stuff ) = ( $thing =~ m{ # Quote-like characters are now {}. ^ # Match at the start of the string. ( [^/]+ ) # Capture everything in the set of all # characters that are not '/' }x # Close the regexp. x modifier allows # freeform (as in non-significant # whitespace). ); print "$stuff\n";
The m{} brackets avoid the leaning toothpick syndrome in the original regexp. Of course if you were matching against a curly bracket the curly brackets wouldn't be all that wonderful as quote delimiters, but for this regexp they work out well. /x modifier added to allow comments and insignificant whitespace (it makes everything prettier).
Dave
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Regex to get everything before first /
by JavaFan (Canon) on Jun 06, 2011 at 19:44 UTC | |
by davido (Cardinal) on Jun 06, 2011 at 20:43 UTC | |
by Anonymous Monk on Nov 11, 2015 at 19:47 UTC | |
|
In Section
Seekers of Perl Wisdom