Text::Glob - match globbing patterns against text


Text-Glob documentation Contained in the Text-Glob distribution.

Index


Code Index:

NAME

Top

Text::Glob - match globbing patterns against text

SYNOPSIS

Top

 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/;
 }

DESCRIPTION

Top

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.

Routines

match_glob( $glob, @things_to_test )

Returns the list of things which match the glob from the source list.

glob_to_regex( $glob )

Returns a compiled regex which is the equivalent of the globbing pattern.

glob_to_regex_string( $glob )

Returns a regex string which is the equivalent of the globbing pattern.

SYNTAX

Top

The following metacharacters and rules are respected.

* - match zero or more characters

a* matches a, aa, aaaa and many many more.

? - match exactly one character

a? matches aa, but not a, or aaa

Character sets/ranges

example.[ch] matches example.c and example.h

demo.[a-c] matches demo.a, demo.b, and demo.c

alternation

example.{foo,bar,baz} matches example.foo, example.bar, and example.baz

leading . must be explictly matched

*.foo does not match .bar.foo. For this you must either specify the leading . in the glob pattern (.*.foo), or set $Text::Glob::strict_leading_dot to a false value while compiling the regex.

* and ? do not match /

*.foo does not match bar/baz.foo. For this you must either explicitly match the / in the glob (*/*.foo), or set $Text::Glob::strict_wildcard_slash to a false value with compiling the regex.

BUGS

Top

The code uses qr// to produce compiled regexes, therefore this module requires perl version 5.005_03 or newer.

AUTHOR

Top

Richard Clamp <richardc@unixbeard.net>

COPYRIGHT

Top

SEE ALSO

Top

File::Glob, glob(3)


Text-Glob documentation Contained in the Text-Glob distribution.

package Text::Glob;
use strict;
use Exporter;
use vars qw/$VERSION @ISA @EXPORT_OK
            $strict_leading_dot $strict_wildcard_slash/;
$VERSION = '0.09';
@ISA = 'Exporter';
@EXPORT_OK = qw( glob_to_regex glob_to_regex_string match_glob );

$strict_leading_dot    = 1;
$strict_wildcard_slash = 1;

use constant debug => 0;

sub glob_to_regex {
    my $glob = shift;
    my $regex = glob_to_regex_string($glob);
    return qr/^$regex$/;
}

sub glob_to_regex_string
{
    my $glob = shift;
    my ($regex, $in_curlies, $escaping);
    local $_;
    my $first_byte = 1;
    for ($glob =~ m/(.)/gs) {
        if ($first_byte) {
            if ($strict_leading_dot) {
                $regex .= '(?=[^\.])' unless $_ eq '.';
            }
            $first_byte = 0;
        }
        if ($_ eq '/') {
            $first_byte = 1;
        }
        if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' ||
            $_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) {
            $regex .= "\\$_";
        }
        elsif ($_ eq '*') {
            $regex .= $escaping ? "\\*" :
              $strict_wildcard_slash ? "[^/]*" : ".*";
        }
        elsif ($_ eq '?') {
            $regex .= $escaping ? "\\?" :
              $strict_wildcard_slash ? "[^/]" : ".";
        }
        elsif ($_ eq '{') {
            $regex .= $escaping ? "\\{" : "(";
            ++$in_curlies unless $escaping;
        }
        elsif ($_ eq '}' && $in_curlies) {
            $regex .= $escaping ? "}" : ")";
            --$in_curlies unless $escaping;
        }
        elsif ($_ eq ',' && $in_curlies) {
            $regex .= $escaping ? "," : "|";
        }
        elsif ($_ eq "\\") {
            if ($escaping) {
                $regex .= "\\\\";
                $escaping = 0;
            }
            else {
                $escaping = 1;
            }
            next;
        }
        else {
            $regex .= $_;
            $escaping = 0;
        }
        $escaping = 0;
    }
    print "# $glob $regex\n" if debug;

    return $regex;
}

sub match_glob {
    print "# ", join(', ', map { "'$_'" } @_), "\n" if debug;
    my $glob = shift;
    my $regex = glob_to_regex $glob;
    local $_;
    grep { $_ =~ $regex } @_;
}

1;
__END__