]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - README
Ignore Debian_CPANTS.txt
[perl/modules/Regexp-Wildcards.git] / README
1 NAME
2     Regexp::Wildcards - Converts wildcard expressions to Perl regular
3     expressions.
4
5 VERSION
6     Version 0.08
7
8 SYNOPSIS
9         use Regexp::Wildcards qw/wc2re/;
10
11         my $re;
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.
16
17 DESCRIPTION
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.
26
27 VARIABLES
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
30
31         $Regexp::Wildcards::CaptureSingle = 1;
32         # From then, "exactly one" wildcards are capturing
33
34     or can be locally specified via "local"
35
36         {
37          local $Regexp::Wildcards::CaptureSingle = 1;
38          # In this block, "exactly one" wildcards are capturing.
39          ...
40         }
41         # Back to the situation from before the block
42
43     This section describes also how those elements are translated by the
44     functions.
45
46   $CaptureSingle
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.
51
52         For jokers :
53         'a???b\\??' is translated to 'a(.)(.)(.)b\\?(.)' if $CaptureSingle is true
54                                      'a...b\\?.'         otherwise (default)
55
56         For SQL wildcards :
57         'a___b\\__' is translated to 'a(.)(.)(.)b\\_(.)' if $CaptureSingle is true
58                                      'a...b\\_.'         otherwise (default)
59
60   $CaptureAny
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 ("(.*?)").
66
67         For jokers :
68         'a***b\\**' is translated to 'a.*b\\*.*'       if $CaptureAny is false (default)
69                                      'a(.*)b\\*(.*)'   if $CaptureAny > 0
70                                      'a(.*?)b\\*(.*?)' otherwise
71
72         For SQL wildcards :
73         'a%%%b\\%%' is translated to 'a.*b\\%.*'       if $CaptureAny is false (default)
74                                      'a(.*)b\\%(.*)'   if $CaptureAny > 0
75                                      'a(.*?)b\\%(.*?)' otherwise
76
77   $CaptureBrackets
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.
81
82         'a{b\\},\\{c}' is translated to 'a(b\\}|\\{c)'   if $CaptureBrackets is true
83                                         'a(?:b\\}|\\{c)' otherwise (default)
84
85 FUNCTIONS
86   "wc2re_jokers"
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.
92
93         # Everything is escaped.
94         print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
95
96   "wc2re_sql"
97     Similar to the precedent, but for the SQL wildcards "_" (*"exactly
98     one"*) and "%" (*"any"*). All other unprotected regexp metacharacters
99     are escaped.
100
101   "wc2re_unix"
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.
110
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)';
113
114     The function handles unbalanced bracket expressions, by escaping
115     everything it can't recognize. For example :
116
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\\}';
119
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\\}';
122
123   "wc2re_win32"
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.
127
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\\})';
130
131   "wc2re"
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 :
135
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"
139         respectively.
140
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.
144
145     If the type is undefined or not supported, it defaults to 'unix'.
146
147          # Wraps to wc2re_jokers ($re eq 'a\\{b\\,c\\}.*').
148          $re = wc2re 'a{b,c}*' => 'jokers';
149
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;
153
154 EXPORT
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.
158
159 DEPENDENCIES
160     Text::Balanced, which is bundled with perl since version 5.7.3
161
162 CAVEATS
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.
167
168 SEE ALSO
169     Some modules provide incomplete alternatives as helper functions :
170
171     Net::FTPServer has a method for that. Only jokers are translated, and
172     escaping won't preserve them.
173
174     File::Find::Match::Util has a "wildcard" function that compiles a
175     matcher. It only handles "*".
176
177     Text::Buffer has the "convertWildcardToRegex" class method that handles
178     jokers.
179
180 AUTHOR
181     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
182
183     You can contact me by mail or on #perl @ FreeNode (vincent or
184     Prof_Vince).
185
186 BUGS
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.
192
193 SUPPORT
194     You can find documentation for this module with the perldoc command.
195
196         perldoc Regexp::Wildcards
197
198 COPYRIGHT & LICENSE
199     Copyright 2007-2008 Vincent Pit, all rights reserved.
200
201     This program is free software; you can redistribute it and/or modify it
202     under the same terms as Perl itself.
203