]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - README
This is 1.04
[perl/modules/Regexp-Wildcards.git] / README
1 NAME
2     Regexp::Wildcards - Converts wildcard expressions to Perl regular
3     expressions.
4
5 VERSION
6     Version 1.04
7
8 SYNOPSIS
9         use Regexp::Wildcards;
10
11         my $rw = Regexp::Wildcards->new(type => 'unix');
12
13         my $re;
14         $re = $rw->convert('a{b?,c}*');          # Do it Unix shell style.
15         $re = $rw->convert('a?,b*',   'win32');  # Do it Windows shell style.
16         $re = $rw->convert('*{x,y}?', 'jokers'); # Process the jokers and escape the rest.
17         $re = $rw->convert('%a_c%',   'sql');    # Turn SQL wildcards into regexps.
18
19         $rw = Regexp::Wildcards->new(
20          do      => [ qw<jokers brackets> ], # Do jokers and brackets.
21          capture => [ qw<any greedy> ],      # Capture *'s greedily.
22         );
23
24         $rw->do(add => 'groups');            # Don't escape groups.
25         $rw->capture(rem => [ qw<greedy> ]); # Actually we want non-greedy matches.
26         $re = $rw->convert('*a{,(b)?}?c*');  # '(.*?)a(?:|(b).).c(.*?)'
27         $rw->capture();                      # No more captures.
28
29 DESCRIPTION
30     In many situations, users may want to specify patterns to match but
31     don't need the full power of regexps. Wildcards make one of those sets
32     of simplified rules. This module converts wildcard expressions to Perl
33     regular expressions, so that you can use them for matching.
34
35     It handles the "*" and "?" jokers, as well as Unix bracketed
36     alternatives "{,}", but also "%" and "_" SQL wildcards. If required, it
37     can also keep original "(...)" groups or "^" and "$" anchors. Backspace
38     ("\") is used as an escape character.
39
40     Typesets that mimic the behaviour of Windows and Unix shells are also
41     provided.
42
43 METHODS
44   "new [ do => $what | type => $type ], capture => $captures"
45     Constructs a new Regexp::Wildcard object.
46
47     "do" lists all features that should be enabled when converting wildcards
48     to regexps. Refer to "do" for details on what can be passed in $what.
49
50     The "type" specifies a predefined set of "do" features to use. See
51     "type" for details on which types are valid. The "do" option overrides
52     "type".
53
54     "capture" lists which atoms should be capturing. Refer to "capture" for
55     more details.
56
57   "do [ $what | set => $c1, add => $c2, rem => $c3 ]"
58     Specifies the list of metacharacters to convert or to prevent for
59     escaping. They fit into six classes :
60
61     *   'jokers'
62
63         Converts "?" to "." and "*" to ".*".
64
65             'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c'
66
67     *   'sql'
68
69         Converts "_" to "." and "%" to ".*".
70
71             'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c'
72
73     *   'commas'
74
75         Converts all "," to "|" and puts the complete resulting regular
76         expression inside "(?: ... )".
77
78             'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)'
79
80     *   'brackets'
81
82         Converts all matching "{ ... , ... }" brackets to "(?: ... | ... )"
83         alternations. If some brackets are unbalanced, it tries to
84         substitute as many of them as possible, and then escape the
85         remaining unmatched "{" and "}". Commas outside of any
86         bracket-delimited block are also escaped.
87
88             'a,b{c,d},e'    ==> 'a\\,b(?:c|d)\\,e'
89             '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}'
90             '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}'
91
92     *   'groups'
93
94         Keeps the parenthesis "( ... )" of the original string without
95         escaping them. Currently, no check is done to ensure that the
96         parenthesis are matching.
97
98             'a(b(c))d\\(\\)' ==> (no change)
99
100     *   'anchors'
101
102         Prevents the *beginning-of-line* "^" and *end-of-line* "$" anchors
103         to be escaped. Since "[...]" character class are currently escaped,
104         a "^" will always be interpreted as *beginning-of-line*.
105
106             'a^b$c' ==> (no change)
107
108     Each $c can be any of :
109
110     *   A hash reference, with wanted metacharacter group names (described
111         above) as keys and booleans as values ;
112
113     *   An array reference containing the list of wanted metacharacter
114         classes ;
115
116     *   A plain scalar, when only one group is required.
117
118     When "set" is present, the classes given as its value replace the
119     current object options. Then the "add" classes are added, and the "rem"
120     classes removed.
121
122     Passing a sole scalar $what is equivalent as passing "set => $what". No
123     argument means "set => [ ]".
124
125         $rw->do(set => 'jokers');           # Only translate jokers.
126         $rw->do('jokers');                  # Same.
127         $rw->do(add => [ qw<sql commas> ]); # Translate also SQL and commas.
128         $rw->do(rem => 'jokers');           # Specifying both 'sql' and 'jokers' is useless.
129         $rw->do();                          # Translate nothing.
130
131     The "do" method returns the Regexp::Wildcards object.
132
133   "type $type"
134     Notifies to convert the metacharacters that corresponds to the
135     predefined type $type. $type can be any of :
136
137     *   'jokers', 'sql', 'commas', 'brackets'
138
139         Singleton types that enable the corresponding "do" classes.
140
141     *   'unix'
142
143         Covers typical Unix shell globbing features (effectively 'jokers'
144         and 'brackets').
145
146     *   $^O values for common Unix systems
147
148         Wrap to 'unix' (see perlport for the list).
149
150     *   "undef"
151
152         Defaults to 'unix'.
153
154     *   'win32'
155
156         Covers typical Windows shell globbing features (effectively 'jokers'
157         and 'commas').
158
159     *   'dos', 'os2', 'MSWin32', 'cygwin'
160
161         Wrap to 'win32'.
162
163     In particular, you can usually pass $^O as the $type and get the
164     corresponding shell behaviour.
165
166         $rw->type('win32'); # Set type to win32.
167         $rw->type($^O);     # Set type to unix on Unices and win32 on Windows
168         $rw->type();        # Set type to unix.
169
170     The "type" method returns the Regexp::Wildcards object.
171
172   "capture [ $captures | set => $c1, add => $c2, rem => $c3 ]"
173     Specifies the list of atoms to capture. This method works like "do",
174     except that the classes are different :
175
176     *   'single'
177
178         Captures all unescaped *"exactly one"* metacharacters, i.e. "?" for
179         wildcards or "_" for SQL.
180
181             'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)'
182             'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)'
183
184     *   'any'
185
186         Captures all unescaped *"any"* metacharacters, i.e. "*" for
187         wildcards or "%" for SQL.
188
189             'a***b\\**' ==> 'a(.*)b\\*(.*)'
190             'a%%%b\\%%' ==> 'a(.*)b\\%(.*)'
191
192     *   'greedy'
193
194         When used in conjunction with 'any', it makes the 'any' captures
195         greedy (by default they are not).
196
197             'a***b\\**' ==> 'a(.*?)b\\*(.*?)'
198             'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)'
199
200     *   'brackets'
201
202         Capture matching "{ ... , ... }" alternations.
203
204             'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)'
205
206         $rw->capture(set => 'single');           # Only capture "exactly one" metacharacters.
207         $rw->capture('single');                  # Same.
208         $rw->capture(add => [ qw<any greedy> ]); # Also greedily capture "any" metacharacters.
209         $rw->capture(rem => 'greedy');           # No more greed please.
210         $rw->capture();                          # Capture nothing.
211
212     The "capture" method returns the Regexp::Wildcards object.
213
214   "convert $wc [ , $type ]"
215     Converts the wildcard expression $wc into a regular expression according
216     to the options stored into the Regexp::Wildcards object, or to $type if
217     it's supplied. It successively escapes all unprotected regexp special
218     characters that doesn't hold any meaning for wildcards, then replace
219     'jokers', 'sql' and 'commas' or 'brackets' (depending on the "do" or
220     "type" options), all of this by applying the 'capture' rules specified
221     in the constructor or by "capture".
222
223 EXPORT
224     An object module shouldn't export any function, and so does this one.
225
226 DEPENDENCIES
227     Carp (core module since perl 5), Scalar::Util, Text::Balanced (since
228     5.7.3).
229
230 CAVEATS
231     This module does not implement the strange behaviours of Windows shell
232     that result from the special handling of the three last characters (for
233     the file extension). For example, Windows XP shell matches *a like
234     ".*a", "*a?" like ".*a.?", "*a??" like ".*a.{0,2}" and so on.
235
236 SEE ALSO
237     Text::Glob.
238
239 AUTHOR
240     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
241
242     You can contact me by mail or on "irc.perl.org" (vincent).
243
244 BUGS
245     Please report any bugs or feature requests to "bug-regexp-wildcards at
246     rt.cpan.org", or through the web interface at
247     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
248     will be notified, and then you'll automatically be notified of progress
249     on your bug as I make changes.
250
251 SUPPORT
252     You can find documentation for this module with the perldoc command.
253
254         perldoc Regexp::Wildcards
255
256     Tests code coverage report is available at
257     <http://www.profvince.com/perl/cover/Regexp-Wildcards>.
258
259 COPYRIGHT & LICENSE
260     Copyright 2007-2009 Vincent Pit, all rights reserved.
261
262     This program is free software; you can redistribute it and/or modify it
263     under the same terms as Perl itself.
264