4 use vars qw/$VERSION @ISA @EXPORT_OK
5 $strict_leading_dot $strict_wildcard_slash/;
8 @EXPORT_OK = qw( glob_to_regex glob_to_regex_string match_glob );
10 $strict_leading_dot = 1;
11 $strict_wildcard_slash = 1;
13 use constant debug => 0;
17 my $regex = glob_to_regex_string($glob);
21 sub glob_to_regex_string
24 my ($regex, $in_curlies, $escaping);
27 for ($glob =~ m/(.)/gs) {
29 if ($strict_leading_dot) {
30 $regex .= '(?=[^\.])' unless $_ eq '.';
37 if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' ||
38 $_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) {
42 $regex .= $escaping ? "\\*" :
43 $strict_wildcard_slash ? "[^/]*" : ".*";
46 $regex .= $escaping ? "\\?" :
47 $strict_wildcard_slash ? "[^/]" : ".";
50 $regex .= $escaping ? "\\{" : "(";
51 ++$in_curlies unless $escaping;
53 elsif ($_ eq '}' && $in_curlies) {
54 $regex .= $escaping ? "}" : ")";
55 --$in_curlies unless $escaping;
57 elsif ($_ eq ',' && $in_curlies) {
58 $regex .= $escaping ? "," : "|";
76 print "# $glob $regex\n" if debug;
82 print "# ", join(', ', map { "'$_'" } @_), "\n" if debug;
84 my $regex = glob_to_regex $glob;
86 grep { $_ =~ $regex } @_;
94 Text::Glob - match globbing patterns against text
98 use Text::Glob qw( match_glob glob_to_regex );
100 print "matched\n" if match_glob( "foo.*", "foo.bar" );
102 # prints foo.bar and foo.baz
103 my $regex = glob_to_regex( "foo.*" );
104 for ( qw( foo.bar foo.baz foo bar ) ) {
105 print "matched: $_\n" if /$regex/;
110 Text::Glob implements glob(3) style matching that can be used to match
111 against text, rather than fetching names from a filesystem. If you
112 want to do full file globbing use the File::Glob module instead.
118 =item match_glob( $glob, @things_to_test )
120 Returns the list of things which match the glob from the source list.
122 =item glob_to_regex( $glob )
124 Returns a compiled regex which is the equiavlent of the globbing
127 =item glob_to_regex_string( $glob )
129 Returns a regex string which is the equiavlent of the globbing
136 The following metacharacters and rules are respected.
140 =item C<*> - match zero or more characters
142 C<a*> matches C<a>, C<aa>, C<aaaa> and many many more.
144 =item C<?> - match exactly one character
146 C<a?> matches C<aa>, but not C<a>, or C<aa>
148 =item Character sets/ranges
150 C<example.[ch]> matches C<example.c> and C<example.h>
152 C<demo.[a-c]> matches C<demo.a>, C<demo.b>, and C<demo.c>
156 C<example.{foo,bar,baz}> matches C<example.foo>, C<example.bar>, and
159 =item leading . must be explictly matched
161 C<*.foo> does not match C<.bar.foo>. For this you must either specify
162 the leading . in the glob pattern (C<.*.foo>), or set
163 C<$Text::Glob::strict_leading_dot> to a false value while compiling
166 =item C<*> and C<?> do not match /
168 C<*.foo> does not match C<bar/baz.foo>. For this you must either
169 explicitly match the / in the glob (C<*/*.foo>), or set
170 C<$Text::Glob::strict_wildcard_slash> to a false value with compiling
177 The code uses qr// to produce compiled regexes, therefore this module
178 requires perl version 5.005_03 or newer.
182 Richard Clamp <richardc@unixbeard.net>
186 Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved.
188 This module is free software; you can redistribute it and/or modify it
189 under the same terms as Perl itself.
193 L<File::Glob>, glob(3)