tags:

views:

61

answers:

2

How to match strings in shell-style in Perl? For instance:

foo*
{bar,baz}.smth
joe?
foobar[0-9]
+5  A: 

Using regular expressions

Your examples would be:

/foo.*(bar|baz)\.smth joe/

/foobar\d/

However, if what you actually wanted was shell-like filename expansion (e.g. the above was in context of ls foobar[0-9] ), use glob() function:

my @files = glob("foo* {bar,baz}.smth joe");

my @foobar_files = glob("foobar[0-9]");

Please note that the syntax of regular expressions in Perl is NOT that of filename expansion language

DVK
A: 

File::FnMatch exposes your system's fnmatch(3) implementation, which is probably what implements the shell's wildcard patterns for you.

(Note that {bar,baz}.smth is not matching per se, it is simply "brace expansion" of strings.)

pilcrow