I find the following statement in a perl (actually PDL) program:
/\/([\w]+)$/i;
Can someone decode this for me, an apprentice in perl programming?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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,\wwill 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.