Match globbing patterns against text
use Text::Glob qw( match_glob glob_to_regex ); print "matched\n" if match_glob( "foo.*", "foo.bar" ); # prints foo.bar and foo.baz my $regex = glob_to_regex( "foo.*" ); for ( qw( foo.bar foo.baz foo bar ) ) { print "matched: $_\n" if /$regex/; }
Text::Glob implements glob\|(3) style matching that can be used to match against text, rather than fetching names from a filesystem. If you want to do full file globbing use the File::Glob module instead.
Returns the list of things which match the glob from the source list. Returns a compiled regex which is the equivalent of the globbing pattern. Returns a regex string which is the equivalent of the globbing pattern.
The following metacharacters and rules are respected. \*(C`a*\*(C' matches \*(C`a\*(C', \*(C`aa\*(C', \*(C`aaaa\*(C' and many many more. \*(C`a?\*(C' matches \*(C`aa\*(C', but not \*(C`a\*(C', or \*(C`aaa\*(C'
\*(C`example.[ch]\*(C' matches \*(C`example.c\*(C' and \*(C`example.h\*(C' \*(C`demo.[a-c]\*(C' matches \*(C`demo.a\*(C', \*(C`demo.b\*(C', and \*(C`demo.c\*(C'
\*(C`example.{foo,bar,baz}\*(C' matches \*(C`example.foo\*(C', \*(C`example.bar\*(C', and \*(C`example.baz\*(C'
\*(C`*.foo\*(C' does not match \*(C`.bar.foo\*(C'. For this you must either specify the leading . in the glob pattern (\*(C`.*.foo\*(C'), or set $Text::Glob::strict_leading_dot to a false value while compiling the regex. \*(C`*.foo\*(C' does not match \*(C`bar/baz.foo\*(C'. For this you must either explicitly match the / in the glob (\*(C`*/*.foo\*(C'), or set $Text::Glob::strict_wildcard_slash to a false value with compiling the regex.
The code uses qr// to produce compiled regexes, therefore this module requires perl version 5.005_03 or newer.
Richard Clamp <[email protected]>
Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
File::Glob, glob\|(3)