File: README

package info (click to toggle)
libregexp-wildcards-perl 1.05-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: perl: 489; makefile: 2
file content (288 lines) | stat: -rw-r--r-- 9,797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
NAME
    Regexp::Wildcards - Converts wildcard expressions to Perl regular
    expressions.

VERSION
    Version 1.05

SYNOPSIS
        use Regexp::Wildcards;

        my $rw = Regexp::Wildcards->new(type => 'unix');

        my $re;
        $re = $rw->convert('a{b?,c}*');          # Do it Unix shell style.
        $re = $rw->convert('a?,b*',   'win32');  # Do it Windows shell style.
        $re = $rw->convert('*{x,y}?', 'jokers'); # Process the jokers and
                                                 # escape the rest.
        $re = $rw->convert('%a_c%',   'sql');    # Turn SQL wildcards into
                                                 # regexps.

        $rw = Regexp::Wildcards->new(
         do      => [ qw<jokers brackets> ], # Do jokers and brackets.
         capture => [ qw<any greedy> ],      # Capture *'s greedily.
        );

        $rw->do(add => 'groups');            # Don't escape groups.
        $rw->capture(rem => [ qw<greedy> ]); # Actually we want non-greedy
                                             # matches.
        $re = $rw->convert('*a{,(b)?}?c*');  # '(.*?)a(?:|(b).).c(.*?)'
        $rw->capture();                      # No more captures.

DESCRIPTION
    In many situations, users may want to specify patterns to match but
    don't need the full power of regexps. Wildcards make one of those sets
    of simplified rules. This module converts wildcard expressions to Perl
    regular expressions, so that you can use them for matching.

    It handles the "*" and "?" jokers, as well as Unix bracketed
    alternatives "{,}", but also "%" and "_" SQL wildcards. If required, it
    can also keep original "(...)" groups or "^" and "$" anchors. Backspace
    ("\") is used as an escape character.

    Typesets that mimic the behaviour of Windows and Unix shells are also
    provided.

METHODS
  "new"
        my $rw = Regexp::Wildcards->new(do => $what, capture => $capture);
        my $rw = Regexp::Wildcards->new(type => $type, capture => $capture);

    Constructs a new Regexp::Wildcard object.

    "do" lists all features that should be enabled when converting wildcards
    to regexps. Refer to "do" for details on what can be passed in $what.

    The "type" specifies a predefined set of "do" features to use. See
    "type" for details on which types are valid. The "do" option overrides
    "type".

    "capture" lists which atoms should be capturing. Refer to "capture" for
    more details.

  "do"
        $rw->do($what);
        $rw->do(set => $c1);
        $rw->do(add => $c2);
        $rw->do(rem => $c3);

    Specifies the list of metacharacters to convert or to prevent for
    escaping. They fit into six classes :

    *   'jokers'

        Converts "?" to "." and "*" to ".*".

            'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c'

    *   'sql'

        Converts "_" to "." and "%" to ".*".

            'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c'

    *   'commas'

        Converts all "," to "|" and puts the complete resulting regular
        expression inside "(?: ... )".

            'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)'

    *   'brackets'

        Converts all matching "{ ... , ... }" brackets to "(?: ... | ... )"
        alternations. If some brackets are unbalanced, it tries to
        substitute as many of them as possible, and then escape the
        remaining unmatched "{" and "}". Commas outside of any
        bracket-delimited block are also escaped.

            'a,b{c,d},e'    ==> 'a\\,b(?:c|d)\\,e'
            '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}'
            '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}'

    *   'groups'

        Keeps the parenthesis "( ... )" of the original string without
        escaping them. Currently, no check is done to ensure that the
        parenthesis are matching.

            'a(b(c))d\\(\\)' ==> (no change)

    *   'anchors'

        Prevents the *beginning-of-line* "^" and *end-of-line* "$" anchors
        to be escaped. Since "[...]" character class are currently escaped,
        a "^" will always be interpreted as *beginning-of-line*.

            'a^b$c' ==> (no change)

    Each $c can be any of :

    *   A hash reference, with wanted metacharacter group names (described
        above) as keys and booleans as values ;

    *   An array reference containing the list of wanted metacharacter
        classes ;

    *   A plain scalar, when only one group is required.

    When "set" is present, the classes given as its value replace the
    current object options. Then the "add" classes are added, and the "rem"
    classes removed.

    Passing a sole scalar $what is equivalent as passing "set => $what". No
    argument means "set => [ ]".

        $rw->do(set => 'jokers');           # Only translate jokers.
        $rw->do('jokers');                  # Same.
        $rw->do(add => [ qw<sql commas> ]); # Translate also SQL and commas.
        $rw->do(rem => 'jokers');           # Specifying both 'sql' and
                                            # 'jokers' is useless.
        $rw->do();                          # Translate nothing.

    The "do" method returns the Regexp::Wildcards object.

  "type"
        $rw->type($type);

    Notifies to convert the metacharacters that corresponds to the
    predefined type $type. $type can be any of :

    *   'jokers', 'sql', 'commas', 'brackets'

        Singleton types that enable the corresponding "do" classes.

    *   'unix'

        Covers typical Unix shell globbing features (effectively 'jokers'
        and 'brackets').

    *   $^O values for common Unix systems

        Wrap to 'unix' (see perlport for the list).

    *   "undef"

        Defaults to 'unix'.

    *   'win32'

        Covers typical Windows shell globbing features (effectively 'jokers'
        and 'commas').

    *   'dos', 'os2', 'MSWin32', 'cygwin'

        Wrap to 'win32'.

    In particular, you can usually pass $^O as the $type and get the
    corresponding shell behaviour.

        $rw->type('win32'); # Set type to win32.
        $rw->type($^O);     # Set type to unix on Unices and win32 on Windows
        $rw->type();        # Set type to unix.

    The "type" method returns the Regexp::Wildcards object.

  "capture"
        $rw->capture($captures);
        $rw->capture(set => $c1);
        $rw->capture(add => $c2);
        $rw->capture(rem => $c3);

    Specifies the list of atoms to capture. This method works like "do",
    except that the classes are different :

    *   'single'

        Captures all unescaped *"exactly one"* metacharacters, i.e. "?" for
        wildcards or "_" for SQL.

            'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)'
            'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)'

    *   'any'

        Captures all unescaped *"any"* metacharacters, i.e. "*" for
        wildcards or "%" for SQL.

            'a***b\\**' ==> 'a(.*)b\\*(.*)'
            'a%%%b\\%%' ==> 'a(.*)b\\%(.*)'

    *   'greedy'

        When used in conjunction with 'any', it makes the 'any' captures
        greedy (by default they are not).

            'a***b\\**' ==> 'a(.*?)b\\*(.*?)'
            'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)'

    *   'brackets'

        Capture matching "{ ... , ... }" alternations.

            'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)'

        $rw->capture(set => 'single');           # Only capture "exactly one"
                                                 # metacharacters.
        $rw->capture('single');                  # Same.
        $rw->capture(add => [ qw<any greedy> ]); # Also greedily capture
                                                 # "any" metacharacters.
        $rw->capture(rem => 'greedy');           # No more greed please.
        $rw->capture();                          # Capture nothing.

    The "capture" method returns the Regexp::Wildcards object.

  "convert"
        my $rx = $rw->convert($wc);
        my $rx = $rw->convert($wc, $type);

    Converts the wildcard expression $wc into a regular expression according
    to the options stored into the Regexp::Wildcards object, or to $type if
    it's supplied. It successively escapes all unprotected regexp special
    characters that doesn't hold any meaning for wildcards, then replace
    'jokers', 'sql' and 'commas' or 'brackets' (depending on the "do" or
    "type" options), all of this by applying the 'capture' rules specified
    in the constructor or by "capture".

EXPORT
    An object module shouldn't export any function, and so does this one.

DEPENDENCIES
    Carp (core module since perl 5), Scalar::Util, Text::Balanced (since
    5.7.3).

CAVEATS
    This module does not implement the strange behaviours of Windows shell
    that result from the special handling of the three last characters (for
    the file extension). For example, Windows XP shell matches *a like
    ".*a", "*a?" like ".*a.?", "*a??" like ".*a.{0,2}" and so on.

SEE ALSO
    Text::Glob.

AUTHOR
    Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.

    You can contact me by mail or on "irc.perl.org" (vincent).

BUGS
    Please report any bugs or feature requests to "bug-regexp-wildcards at
    rt.cpan.org", or through the web interface at
    <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I
    will be notified, and then you'll automatically be notified of progress
    on your bug as I make changes.

SUPPORT
    You can find documentation for this module with the perldoc command.

        perldoc Regexp::Wildcards

    Tests code coverage report is available at
    <http://www.profvince.com/perl/cover/Regexp-Wildcards>.

COPYRIGHT & LICENSE
    Copyright 2007,2008,2009,2013 Vincent Pit, all rights reserved.

    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.