cryptic perl expression

I find the following statement in a perl (actually PDL) program:

/\/([\w]+)$/i;

Can someone decode this for me, an apprentice in perl programming?

Answers


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.


Need Your Help

How to disable some Log functions when building an Android app?

android logging

I use Log functions to debug my Android app. I'm close to releasing my app and I'd like to reduce debug level. However I don't know how to disable some of these function. Say I just want to disable

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.