4
use vars qw/$VERSION @ISA @EXPORT_OK
5
$strict_leading_dot $strict_wildcard_slash/;
8
@EXPORT_OK = qw( glob_to_regex match_glob );
10
$strict_leading_dot = 1;
11
$strict_wildcard_slash = 1;
13
use constant debug => 0;
17
my ($regex, $in_curlies, $escaping);
20
for ($glob =~ m/(.)/gs) {
22
if ($strict_leading_dot) {
23
$regex .= '(?=[^\.])' unless $_ eq '.';
30
if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' ||
31
$_ eq '+' || $_ eq '^' || $_ eq '$' ) {
35
$regex .= $escaping ? "\\*" :
36
$strict_wildcard_slash ? "[^/]*" : ".*";
39
$regex .= $escaping ? "\\?" :
40
$strict_wildcard_slash ? "[^/]" : ".";
43
$regex .= $escaping ? "\\{" : "(";
44
++$in_curlies unless $escaping;
46
elsif ($_ eq '}' && $in_curlies) {
47
$regex .= $escaping ? "}" : ")";
48
--$in_curlies unless $escaping;
50
elsif ($_ eq ',' && $in_curlies) {
51
$regex .= $escaping ? "," : "|";
69
print "# $glob $regex\n" if debug;
74
print "# ", join(', ', map { "'$_'" } @_), "\n" if debug;
76
my $regex = glob_to_regex $glob;
78
grep { $_ =~ $regex } @_;
86
Text::Glob - match globbing patterns against text
90
use Text::Glob qw( match_glob glob_to_regex );
92
print "matched\n" if match_glob( "foo.*", "foo.bar" );
94
# prints foo.bar and foo.baz
95
my $regex = glob_to_regex( "foo.*" );
96
for ( qw( foo.bar foo.baz foo bar ) ) {
97
print "matched: $_\n" if /$regex/;
102
Text::Glob implements glob(3) style matching that can be used to match
103
against text, rather than fetching names from a filesystem. If you
104
want to do full file globbing use the File::Glob module instead.
110
=item match_glob( $glob, @things_to_test )
112
Returns the list of things which match the glob from the source list.
114
=item glob_to_regex( $glob )
116
Returns a compiled regex which is the equiavlent of the globbing
123
The following metacharacters and rules are respected.
127
=item C<*> - match zero or more characters
129
C<a*> matches C<a>, C<aa>, C<aaaa> and many many more.
131
=item C<?> - match exactly one character
133
C<a?> matches C<aa>, but not C<a>, or C<aa>
135
=item Character sets/ranges
137
C<example.[ch]> matches C<example.c> and C<example.h>
139
C<demo.[a-c]> matches C<demo.a>, C<demo.b>, and C<demo.c>
143
C<example.{foo,bar,baz}> matches C<example.foo>, C<example.bar>, and
146
=item leading . must be explictly matched
148
C<*.foo> does not match C<.bar.foo>. For this you must either specify
149
the leading . in the glob pattern (C<.*.foo>), or set
150
C<$Text::Glob::strict_leading_dot> to a false value while compiling
153
=item C<*> and C<?> do not match /
155
C<*.foo> does not match C<bar/baz.foo>. For this you must either
156
explicitly match the / in the glob (C<*/*.foo>), or set
157
C<$Text::Glob::strict_wildcard_slash> to a false value with compiling
164
The code uses qr// to produce compiled regexes, therefore this module
165
requires perl version 5.005_03 or newer.
169
Richard Clamp <richardc@unixbeard.net>
173
Copyright (C) 2002 Richard Clamp. All Rights Reserved.
175
This module is free software; you can redistribute it and/or modify it
176
under the same terms as Perl itself.
180
L<File::Glob>, glob(3)