2 Regexp::Wildcards - Converts wildcards expressions to Perl regular
9 use Regexp::Wildcards qw/wc2re/;
12 $re = wc2re 'a{b.,c}*' => 'unix'; # Do it Unix style.
13 $re = wc2re 'a.,b*' => 'win32'; # Do it Windows style.
14 $re = wc2re '*{x,y}.' => 'jokers'; # Process the jokers & escape the rest.
17 In many situations, users may want to specify patterns to match but
18 don't need the full power of regexps. Wildcards make one of those sets
19 of simplified rules. This module converts wildcards expressions to Perl
20 regular expressions, so that you can use them for matching. It handles
21 the "*" and "?" jokers, as well as Unix bracketed alternatives "{,}",
22 and uses the backspace ("\") as an escape character. Wrappers are
23 provided to mimic the behaviour of Windows and Unix shells.
26 Four functions are exported only on request : "wc2re", "wc2re_unix",
27 "wc2re_win32" and "wc2re_jokers".
31 This function takes as its only argument the wildcard string to process,
32 and returns the corresponding regular expression according to standard
33 Unix wildcard rules. It successively escapes all unprotected regexp
34 special characters that doesn't hold any meaning for wildcards, turns
35 jokers into their regexp equivalents, and changes bracketed blocks into
36 "(?:|)" alternations. If brackets are unbalanced, it will try to
37 substitute as many of them as possible, and then escape the remaining
38 "{" and "}". Commas outside of any bracket-delimited block will also be
41 # This is a valid brackets expression which is correctly handled.
42 print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)';
44 Unbalanced bracket expressions can always be rescued, but it may change
45 completely its meaning. For example :
47 # The first comma is replaced, and the remaining brackets and comma are
49 print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}';
51 # All the brackets and commas are escaped.
52 print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
55 Similar to the precedent, but for Windows wildcards. Bracketed blocks
56 are no longer handled (which means that brackets will be escaped), but
57 you can provide a comma-separated list of items.
59 # All the brackets are escaped, and commas are seen as list delimiters.
60 print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})';
63 This one only handles the "?" and "*" jokers. All other unquoted regexp
64 metacharacters will be escaped.
66 # Everything is escaped.
67 print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
70 A generic function that wraps around all the different rules. The first
71 argument is the wildcard expression, and the second one is the type of
72 rules to apply, currently either "unix", "win32" or "jokers". If the
73 type is undefined, it defaults to "unix".
76 Text::Balanced, which is bundled with perl since version 5.7.3
79 Some modules provide incomplete alternatives as helper functions :
81 Net::FTPServer has a method for that. Only jokers are translated, and
82 escaping won't preserve them.
84 File::Find::Match::Util has a "wildcar" function that compiles a
85 matcher. Only handles "*".
87 Text::Buffer has the "convertWildcardToRegex" class method that handles
91 Vincent Pit, "<perl at profvince.com>"
94 Please report any bugs or feature requests to "bug-regexp-wildcards at
95 rt.cpan.org", or through the web interface at
96 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
97 will be notified, and then you'll automatically be notified of progress
98 on your bug as I make changes.
101 You can find documentation for this module with the perldoc command.
103 perldoc Regexp::Wildcards
106 Copyright 2007 Vincent Pit, all rights reserved.
108 This program is free software; you can redistribute it and/or modify it
109 under the same terms as Perl itself.