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.
15 $re = wc2re '%a_c%' => 'sql'; # Turn SQL wildcards into regexps.
18 In many situations, users may want to specify patterns to match but
19 don't need the full power of regexps. Wildcards make one of those sets
20 of simplified rules. This module converts wildcard expressions to Perl
21 regular expressions, so that you can use them for matching. It handles
22 the "*" and "?" shell jokers, as well as Unix bracketed alternatives
23 "{,}", but also "%" and "_" SQL wildcards. Backspace ("\") is used as an
24 escape character. Wrappers are provided to mimic the behaviour of
25 Windows and Unix shells.
28 These variables control if the wildcards jokers and brackets must
29 capture their match. They can be globally set by writing in your program
31 $Regexp::Wildcards::CaptureSingle = 1;
32 # From then, "exactly one" wildcards are capturing
34 or can be locally specified via "local"
37 local $Regexp::Wildcards::CaptureSingle = 1;
38 # In this block, "exactly one" wildcards are capturing.
41 # Back to the situation from before the block
43 This section describes also how those elements are translated by the
47 When this variable is true, each occurence of unescaped *"exactly one"*
48 wildcards (i.e. "?" jokers or "_" for SQL wildcards) are made capturing
49 in the resulting regexp (they are be replaced by "(.)"). Otherwise, they
50 are just replaced by ".". Default is the latter.
53 'a???b\\??' is translated to 'a(.)(.)(.)b\\?(.)' if $CaptureSingle is true
54 'a...b\\?.' otherwise (default)
57 'a___b\\__' is translated to 'a(.)(.)(.)b\\_(.)' if $CaptureSingle is true
58 'a...b\\_.' otherwise (default)
61 By default this variable is false, and successions of unescaped *"any"*
62 wildcards (i.e. "*" jokers or "%" for SQL wildcards) are replaced by one
63 single ".*". When it evalutes to true, those sequences of *"any"*
64 wildcards are made into one capture, which is greedy ("(.*)") for
65 "$CaptureAny > 0" and otherwise non-greedy ("(.*?)").
68 'a***b\\**' is translated to 'a.*b\\*.*' if $CaptureAny is false (default)
69 'a(.*)b\\*(.*)' if $CaptureAny > 0
70 'a(.*?)b\\*(.*?)' otherwise
73 'a%%%b\\%%' is translated to 'a.*b\\%.*' if $CaptureAny is false (default)
74 'a(.*)b\\%(.*)' if $CaptureAny > 0
75 'a(.*?)b\\%(.*?)' otherwise
78 If this variable is set to true, valid brackets constructs are made into
79 "( | )" captures, and otherwise they are replaced by non-capturing
80 alternations ("(?: | ")), which is the default.
82 'a{b\\},\\{c}' is translated to 'a(b\\}|\\{c)' if $CaptureBrackets is true
83 'a(?:b\\}|\\{c)' otherwise (default)
87 This function takes as its only argument the wildcard string to process,
88 and returns the corresponding regular expression where the jokers "?"
89 (*"exactly one"*) and "*" (*"any"*) have been translated into their
90 regexp equivalents (see "VARIABLES" for more details). All other
91 unprotected regexp metacharacters are escaped.
93 # Everything is escaped.
94 print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
97 Similar to the precedent, but for the SQL wildcards "_" (*"exactly
98 one"*) and "%" (*"any"*). All other unprotected regexp metacharacters
102 This function conforms to standard Unix shell wildcard rules. It
103 successively escapes all unprotected regexp special characters that
104 doesn't hold any meaning for wildcards, turns "?" and "*" jokers into
105 their regexp equivalents (see "wc2re_jokers"), and changes bracketed
106 blocks into (possibly capturing) alternations as described in
107 "VARIABLES". If brackets are unbalanced, it tries to substitute as many
108 of them as possible, and then escape the remaining "{" and "}". Commas
109 outside of any bracket-delimited block are also escaped.
111 # This is a valid bracket expression, and is completely translated.
112 print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)';
114 The function handles unbalanced bracket expressions, by escaping
115 everything it can't recognize. For example :
117 # The first comma is replaced, and the remaining brackets and comma are escaped.
118 print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}';
120 # All the brackets and commas are escaped.
121 print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
124 This one works just like the one before, but for Windows wildcards.
125 Bracketed blocks are no longer handled (which means that brackets are
126 escaped), but you can provide a comma-separated list of items.
128 # All the brackets are escaped, and commas are seen as list delimiters.
129 print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})';
132 A generic function that wraps around all the different rules. The first
133 argument is the wildcard expression, and the second one is the type of
134 rules to apply which can be :
136 'unix', 'win32', 'jokers', 'sql'
137 For one of those raw rule names, "wc2re" simply maps to
138 "wc2re_unix", "wc2re_win32", "wc2re_jokers" and "wc2re_sql"
141 $^O If you supply the Perl operating system name, the call is deferred
142 to "wc2re_win32" for $^O equal to 'dos', 'os2', 'MSWin32' or
143 'cygwin', and to "wc2re_unix" in all the other cases.
145 If the type is undefined or not supported, it defaults to 'unix'.
147 # Wraps to wc2re_jokers ($re eq 'a\\{b\\,c\\}.*').
148 $re = wc2re 'a{b,c}*' => 'jokers';
150 # Wraps to wc2re_win32 ($re eq '(?:a\\{b|c\\}.*)')
151 # or wc2re_unix ($re eq 'a(?:b|c).*') depending on $^O.
152 $re = wc2re 'a{b,c}*' => $^O;
155 These five functions are exported only on request : "wc2re",
156 "wc2re_unix", "wc2re_win32", "wc2re_jokers" and "wc2re_sql". The
157 variables are not exported.
160 Text::Balanced, which is bundled with perl since version 5.7.3
163 This module does not implement the strange behaviours of Windows shell
164 that result from the special handling of the three last characters (for
165 the file extension). For example, Windows XP shell matches *a like
166 ".*a", "*a?" like ".*a.?", "*a??" like ".*a.{0,2}" and so on.
169 Some modules provide incomplete alternatives as helper functions :
171 Net::FTPServer has a method for that. Only jokers are translated, and
172 escaping won't preserve them.
174 File::Find::Match::Util has a "wildcard" function that compiles a
175 matcher. It only handles "*".
177 Text::Buffer has the "convertWildcardToRegex" class method that handles
181 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
183 You can contact me by mail or on #perl @ FreeNode (vincent or
187 Please report any bugs or feature requests to "bug-regexp-wildcards at
188 rt.cpan.org", or through the web interface at
189 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
190 will be notified, and then you'll automatically be notified of progress
191 on your bug as I make changes.
194 You can find documentation for this module with the perldoc command.
196 perldoc Regexp::Wildcards
199 Copyright 2007-2008 Vincent Pit, all rights reserved.
201 This program is free software; you can redistribute it and/or modify it
202 under the same terms as Perl itself.