]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - lib/Regexp/Wildcards.pm
a077628649fcd51bade94a1ed3112fc1ce2e58dc
[perl/modules/Regexp-Wildcards.git] / lib / Regexp / Wildcards.pm
1 package Regexp::Wildcards;
2
3 use strict;
4 use warnings;
5
6 use Text::Balanced qw/extract_bracketed/;
7
8 =head1 NAME
9
10 Regexp::Wildcards - Converts wildcard expressions to Perl regular expressions.
11
12 =head1 VERSION
13
14 Version 0.03
15
16 =cut
17
18 our $VERSION = '0.03';
19
20 =head1 SYNOPSIS
21
22     use Regexp::Wildcards qw/wc2re/;
23
24     my $re;
25     $re = wc2re 'a{b.,c}*' => 'unix';   # Do it Unix style.
26     $re = wc2re 'a.,b*'    => 'win32';  # Do it Windows style.
27     $re = wc2re '*{x,y}.'  => 'jokers'; # Process the jokers & escape the rest.
28
29 =head1 DESCRIPTION
30
31 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 C<*> and C<?> jokers, as well as Unix bracketed alternatives C<{,}>, and uses the backspace (C<\>) as an escape character. Wrappers are provided to mimic the behaviour of Windows and Unix shells.
32
33 =head1 FUNCTIONS
34
35 =head2 C<wc2re_unix>
36
37 This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression according to standard Unix wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns jokers into their regexp equivalents, and changes bracketed blocks into C<(?:|)> alternations. If brackets are unbalanced, it will try to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block will also be escaped.
38
39     # This is a valid bracket expression, and is completely translated.
40     print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)';
41
42 The function handles unbalanced bracket expressions, by escaping everything it can't recognize. For example :
43
44     # The first comma is replaced, and the remaining brackets and comma are escaped.
45     print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}';
46
47     # All the brackets and commas are escaped.
48     print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
49
50 =cut
51
52 sub wc2re_unix {
53  my ($re) = @_;
54  return unless defined $re;
55  $re =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\\{\},])/\\$1/g;
56  return do_bracketed(do_jokers($re));
57 }
58
59 =head2 C<wc2re_win32>
60
61 Similar to the precedent, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets will be escaped), but you can provide a comma-separated list of items.
62
63     # All the brackets are escaped, and commas are seen as list delimiters.
64     print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})';
65
66 =cut
67
68 sub wc2re_win32 {
69  my ($wc) = @_;
70  return unless defined $wc;
71  $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\,])/\\$1/g;
72  my $re = do_jokers($wc);
73  if ($re =~ /(?<!\\)(?:\\\\)*,/) { # win32 allows comma-separated lists
74   $re = '(?:' . do_commas($re) . ')';
75  }
76  return $re;
77 }
78
79 =head2 C<wc2re_jokers>
80
81 This one only handles the C<?> and C<*> jokers. All other unquoted regexp metacharacters will be escaped.
82
83     # Everything is escaped.
84     print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
85
86 =cut
87
88 sub wc2re_jokers {
89  my ($wc) = @_;
90  $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\])/\\$1/g;
91  return do_jokers($wc);
92 }
93
94 =head2 C<wc2re>
95
96 A generic function that wraps around all the different rules. The first argument is the wildcard expression, and the second one is the type of rules to apply, currently either C<unix>, C<win32> or C<jokers>. If the type is undefined, it defaults to C<unix>.
97
98 =cut
99
100 my %types = (
101  'jokers' => \&wc2re_jokers,
102  'unix'   => \&wc2re_unix,
103  'win32'  => \&wc2re_win32
104 );
105
106 sub wc2re {
107  my ($wc, $type) = @_;
108  return unless defined $wc;
109  $type ||= 'unix';
110  return $types{lc $type}($wc);
111 }
112
113 =head1 EXPORT
114
115 These four functions are exported only on request : C<wc2re>, C<wc2re_unix>, C<wc2re_win32> and C<wc2re_jokers>.
116
117 =cut
118
119 use base qw/Exporter/;
120
121 our @EXPORT      = ();
122 our @EXPORT_OK   = ('wc2re', map { 'wc2re_' . $_ } keys %types);
123 our @EXPORT_FAIL = qw/extract do_jokers do_commas do_brackets do_bracketed/; 
124 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
125
126 =head1 DEPENDENCIES
127
128 L<Text::Balanced>, which is bundled with perl since version 5.7.3
129
130 =head1 SEE ALSO
131
132 Some modules provide incomplete alternatives as helper functions :
133
134 L<Net::FTPServer> has a method for that. Only jokers are translated, and escaping won't preserve them.
135
136 L<File::Find::Match::Util> has a C<wildcard> function that compiles a matcher. It only handles C<*>.
137
138 L<Text::Buffer> has the C<convertWildcardToRegex> class method that handles jokers.
139
140 =head1 AUTHOR
141
142 Vincent Pit, C<< <perl at profvince.com> >>
143
144 =head1 BUGS
145
146 Please report any bugs or feature requests to
147 C<bug-regexp-wildcards at rt.cpan.org>, or through the web interface at
148 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>.
149 I will be notified, and then you'll automatically be notified of progress on
150 your bug as I make changes.
151
152 =head1 SUPPORT
153
154 You can find documentation for this module with the perldoc command.
155
156     perldoc Regexp::Wildcards
157
158 =head1 COPYRIGHT & LICENSE
159
160 Copyright 2007 Vincent Pit, all rights reserved.
161
162 This program is free software; you can redistribute it and/or modify it
163 under the same terms as Perl itself.
164
165 =cut
166
167 sub extract { extract_bracketed shift, '{',  qr/.*?(?:(?<!\\)(?:\\\\)*)(?={)/; }
168
169 sub do_jokers {
170  local $_ = shift;
171  # escape an odd number of \ that doesn't protect a regexp/wildcard special char
172  s/(?<!\\)((?:\\\\)*\\(?:[\w\s]|$))/\\$1/g;
173  # substitute ? preceded by an even number of \
174  s/(?<!\\)((?:\\\\)*)\?/$1./g;
175  # substitute * preceded by an even number of \
176  s/(?<!\\)((?:\\\\)*)\*+/$1.*/g;
177  return $_;
178 }
179
180 sub do_commas {
181  local $_ = shift;
182  # substitute , preceded by an even number of \
183  s/(?<!\\)((?:\\\\)*),/$1|/g;
184  return $_;
185 }
186
187 sub do_brackets {
188  my $rest = shift;
189  substr $rest, 0, 1, '';
190  chop $rest;
191  my ($re, $bracket, $prefix) = ('');
192  while (($bracket, $rest, $prefix) = extract $rest and $bracket) {
193   $re .= do_commas($prefix) . do_brackets($bracket);
194  }
195  $re .= do_commas($rest);
196  return '(?:' . $re . ')';
197 }
198
199 sub do_bracketed {
200  my $rest = shift;
201  my ($re, $bracket, $prefix) = ('');
202  while (($bracket, $rest, $prefix) = extract $rest and $bracket) {
203   $re .= $prefix . do_brackets($bracket);
204  }
205  $re .= $rest;
206  $re =~ s/(?<!\\)((?:\\\\)*[\{\},])/\\$1/g;
207  return $re;
208 }
209
210 1; # End of Regexp::Wildcards