aleksey.ischenko
aleksey.ischenko

Reputation: 53

How do I match strings in shell-style using Perl?

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

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

Upvotes: 1

Views: 153

Answers (2)

pilcrow
pilcrow

Reputation: 58534

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.)

Upvotes: 0

DVK
DVK

Reputation: 129393

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

Upvotes: 5

Related Questions