Reputation: 8818
I am trying to decode the following Perl regular expression:
$fname =~ /\/([^\/]+)\.txt$/
What are we trying to match for here?
Upvotes: 0
Views: 150
Reputation: 20163
\/([^\/]+)\.txt
This regular expression matches a file name, as it exists in a path
Examples:
\folder\path\file.txt
Nothing is matched.
folder/path/file.txt
file.txt
is matched (and file
is placed in capture group 1: $1
).
/folder/path/file.txt
Again, file.txt
is matched (and file
captured).
You can try it yourself at Debuggex
Upvotes: 3
Reputation: 66947
Here's how you break it down.
\/
- the literal character /
(...)
- followed by a group that will be captured to $1
[ ... ]
- a character class
^
- in a character class, this means take the inversion of the specified set\/
- the literal character /
+
- one or more times\.
- the literal character .
txt
- the literal string txt
$
- the end of the stringSo, in other words, this is trying to match "anything with a /
followed by one or more characters that are not /
, followed by .txt
, followed by the end of the string, and put the part before .txt
into $1
"
Upvotes: 5