]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - README
This is 1.01
[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.01
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. It can also keep
37     original "(...)" groups. Backspace ("\") is used as an escape character.
38
39     Typesets that mimic the behaviour of Windows and Unix shells are also
40     provided.
41
42 METHODS
43   "new [ do => $what | type => $type ], capture => $captures"
44     Constructs a new Regexp::Wildcard object.
45
46     "do" lists all features that should be enabled when converting wildcards
47     to regexps. Refer to "do" for details on what can be passed in $what.
48
49     The "type" specifies a predefined set of "do" features to use. See
50     "type" for details on which types are valid. The "do" option overrides
51     "type".
52
53     "capture" lists which atoms should be capturing. Refer to "capture" for
54     more details.
55
56   "do [ $what | set => $c1, add => $c2, rem => $c3 ]"
57     Specifies the list of metacharacters to convert. They are classified
58     into five classes :
59
60     *   'jokers' converts "?" to "." and "*" to ".*" ;
61
62             'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c'
63
64     *   'sql' converts "_" to "." and "%" to ".*" ;
65
66             'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c'
67
68     *   'commas' converts all "," to "|" and puts the complete resulting
69         regular expression inside "(?: ... )" ;
70
71             'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)'
72
73     *   'brackets' converts all matching "{ ... , ... }" brackets to "(?:
74         ... | ... )" alternations. If some brackets are unbalanced, it tries
75         to substitute as many of them as possible, and then escape the
76         remaining unmatched "{" and "}". Commas outside of any
77         bracket-delimited block are also escaped ;
78
79             'a,b{c,d},e'    ==> 'a\\,b(?:c|d)\\,e'
80             '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}'
81             '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}'
82
83     *   'groups' keeps the parenthesis "( ... )" of the original string
84         without escaping them. Currently, no check is done to ensure that
85         the parenthesis are matching.
86
87             'a(b(c))d\\(\\)' ==> (no change)
88
89     Each $c can be any of :
90
91     *   A hash reference, with wanted metacharacter group names (described
92         above) as keys and booleans as values ;
93
94     *   An array reference containing the list of wanted metacharacter
95         classes ;
96
97     *   A plain scalar, when only one group is required.
98
99     When "set" is present, the classes given as its value replace the
100     current object options. Then the "add" classes are added, and the "rem"
101     classes removed.
102
103     Passing a sole scalar $what is equivalent as passing "set => $what". No
104     argument means "set => [ ]".
105
106         $rw->do(set => 'jokers');           # Only translate jokers.
107         $rw->do('jokers');                  # Same.
108         $rw->do(add => [ qw/sql commas/ ]); # Translate also SQL and commas.
109         $rw->do(rem => 'jokers');           # Specifying both 'sql' and 'jokers' is useless.
110         $rw->do();                          # Translate nothing.
111
112   "type $type"
113     Notifies to convert the metacharacters that corresponds to the
114     predefined type $type. $type can be any of 'jokers', 'sql', 'commas',
115     'brackets', 'win32' or 'unix'. An unknown or undefined value defaults to
116     'unix', except for 'dos', 'os2', 'MSWin32' and 'cygwin' that default to
117     'win32'. This means that you can pass $^O as the $type and get the
118     corresponding shell behaviour. Returns the object.
119
120         $rw->type('win32'); # Set type to win32.
121         $rw->type();        # Set type to unix.
122
123   "capture [ $captures | set => $c1, add => $c2, rem => $c3 ]"
124     Specifies the list of atoms to capture. This method works like "do",
125     except that the classes are different :
126
127     *   'single' will capture all unescaped *"exactly one"* metacharacters,
128         i.e. "?" for wildcards or "_" for SQL ;
129
130             'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)'
131             'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)'
132
133     *   'any' will capture all unescaped *"any"* metacharacters, i.e. "*"
134         for wildcards or "%" for SQL ;
135
136             'a***b\\**' ==> 'a(.*)b\\*(.*)'
137             'a%%%b\\%%' ==> 'a(.*)b\\%(.*)'
138
139     *   'greedy', when used in conjunction with 'any', will make the 'any'
140         captures greedy (by default they are not) ;
141
142             'a***b\\**' ==> 'a(.*?)b\\*(.*?)'
143             'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)'
144
145     *   'brackets' will capture matching "{ ... , ... }" alternations.
146
147             'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)'
148
149         $rw->capture(set => 'single');           # Only capture "exactly one" metacharacters.
150         $rw->capture('single');                  # Same.
151         $rw->capture(add => [ qw/any greedy/ ]); # Also greedily capture "any" metacharacters.
152         $rw->capture(rem => 'greedy');           # No more greed please.
153         $rw->capture();                          # Capture nothing.
154
155   "convert $wc [ , $type ]"
156     Converts the wildcard expression $wc into a regular expression according
157     to the options stored into the Regexp::Wildcards object, or to $type if
158     it's supplied. It successively escapes all unprotected regexp special
159     characters that doesn't hold any meaning for wildcards, then replace
160     'jokers' or 'sql' and 'commas' or 'brackets' (depending on the "do" or
161     "type" options), all of this by applying the 'capture' rules specified
162     in the constructor or by "capture".
163
164 EXPORT
165     An object module shouldn't export any function, and so does this one.
166
167 DEPENDENCIES
168     Carp (core module since perl 5), Text::Balanced (since 5.7.3).
169
170 CAVEATS
171     This module does not implement the strange behaviours of Windows shell
172     that result from the special handling of the three last characters (for
173     the file extension). For example, Windows XP shell matches *a like
174     ".*a", "*a?" like ".*a.?", "*a??" like ".*a.{0,2}" and so on.
175
176 AUTHOR
177     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
178
179     You can contact me by mail or on #perl @ FreeNode (vincent or
180     Prof_Vince).
181
182 BUGS
183     Please report any bugs or feature requests to "bug-regexp-wildcards at
184     rt.cpan.org", or through the web interface at
185     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
186     will be notified, and then you'll automatically be notified of progress
187     on your bug as I make changes.
188
189 SUPPORT
190     You can find documentation for this module with the perldoc command.
191
192         perldoc Regexp::Wildcards
193
194     Tests code coverage report is available at
195     <http://www.profvince.com/perl/cover/Regexp-Wildcards>.
196
197 COPYRIGHT & LICENSE
198     Copyright 2007-2008 Vincent Pit, all rights reserved.
199
200     This program is free software; you can redistribute it and/or modify it
201     under the same terms as Perl itself.
202