How do I match strings in shell-style using Perl?
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.)
精彩评论