I find the following statement in a perl (actually PDL) program:
/\/([\w]+)$/i;
Can someone decode this for me, an apprentice in perl programming?
Sure, I'll explain it from the inside out:
\w
- matches a single character that can be used in a word (alphanumeric, plus '_')
[...]
- matches a single character from within the brackets
[\w]
- matches a single character that can be used in a word (kinda redundant here)
+
- matches the previous character, repeating as many times as possible, but must appear at least once.
[\w]+
- matches a group of word characters, many times over. This will find a word.
(...)
- grouping. remember this set of characters for later.
([\w]+)
- match a word, and remember it for later
$
- end-of-line. match something at the end of a line
([\w]+)$
- match the last word on a line, and remember it for later
\/
- a single slash character '/'. it must be escaped by backslash, because slash is special.
\/([\w]+)$
- match the last word on a line, after a slash '/', and remember the word for later. This is probably grabbing the directory/file name from a path.
/.../
- match syntax
/.../i
- i means case-insensitive.
All together now:
/\/([\w]+)$/i;
- match the last word on a line and remember it for later; the word must come after a slash. Basically, grab the filename from an absolute path. The case insensitive part is irrelevant, \w
will already match both cases.
More details about Perl regex here: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
And as JRFerguson pointed out, YAPE::Regex::Explain is useful for tokenizing regex, and explaining the pieces.