2 Regexp::Wildcards - Converts wildcard 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 wildcard 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.
27 This function takes as its only argument the wildcard string to process,
28 and returns the corresponding regular expression according to standard
29 Unix wildcard rules. It successively escapes all unprotected regexp
30 special characters that doesn't hold any meaning for wildcards, turns
31 jokers into their regexp equivalents, and changes bracketed blocks into
32 "(?:|)" alternations. If brackets are unbalanced, it will try to
33 substitute as many of them as possible, and then escape the remaining
34 "{" and "}". Commas outside of any bracket-delimited block will also be
37 # This is a valid bracket expression, and is completely translated.
38 print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)';
40 The function handles unbalanced bracket expressions, by escaping
41 everything it can't recognize. For example :
43 # The first comma is replaced, and the remaining brackets and comma are escaped.
44 print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}';
46 # All the brackets and commas are escaped.
47 print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
50 Similar to the precedent, but for Windows wildcards. Bracketed blocks
51 are no longer handled (which means that brackets will be escaped), but
52 you can provide a comma-separated list of items.
54 # All the brackets are escaped, and commas are seen as list delimiters.
55 print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})';
58 This one only handles the "?" and "*" jokers. All other unquoted regexp
59 metacharacters will be escaped.
61 # Everything is escaped.
62 print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
65 A generic function that wraps around all the different rules. The first
66 argument is the wildcard expression, and the second one is the type of
67 rules to apply, currently either "unix", "win32" or "jokers". If the
68 type is undefined, it defaults to "unix".
71 These four functions are exported only on request : "wc2re",
72 "wc2re_unix", "wc2re_win32" and "wc2re_jokers".
75 Text::Balanced, which is bundled with perl since version 5.7.3
78 Some modules provide incomplete alternatives as helper functions :
80 Net::FTPServer has a method for that. Only jokers are translated, and
81 escaping won't preserve them.
83 File::Find::Match::Util has a "wildcard" function that compiles a
84 matcher. It only handles "*".
86 Text::Buffer has the "convertWildcardToRegex" class method that handles
90 Vincent Pit, "<perl at profvince.com>"
93 Please report any bugs or feature requests to "bug-regexp-wildcards at
94 rt.cpan.org", or through the web interface at
95 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
96 will be notified, and then you'll automatically be notified of progress
97 on your bug as I make changes.
100 You can find documentation for this module with the perldoc command.
102 perldoc Regexp::Wildcards
105 Copyright 2007 Vincent Pit, all rights reserved.
107 This program is free software; you can redistribute it and/or modify it
108 under the same terms as Perl itself.