开发者

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

开发者 https://www.devze.com 2023-01-02 07:11 出处:网络
How to match strings i开发者_如何学编程n shell-style in Perl? For instance: foo* {bar,baz}.smth

How to match strings i开发者_如何学编程n shell-style in Perl? For instance:

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消