X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FRegexp-Wildcards.git;a=blobdiff_plain;f=lib%2FRegexp%2FWildcards.pm;h=041223b6170816be7641508a551f57f09d6df635;hp=a3611768621b6d813ff74c31e9a85f3f5ce47b02;hb=3426f55fba3da9c8801930f82107eeac9f1f8339;hpb=03007c83b1d852b1f2120c7aa500c36607d30e72 diff --git a/lib/Regexp/Wildcards.pm b/lib/Regexp/Wildcards.pm index a361176..041223b 100644 --- a/lib/Regexp/Wildcards.pm +++ b/lib/Regexp/Wildcards.pm @@ -11,11 +11,11 @@ Regexp::Wildcards - Converts wildcard expressions to Perl regular expressions. =head1 VERSION -Version 0.05 +Version 0.06 =cut -our $VERSION = '0.05'; +our $VERSION = '0.06'; =head1 SYNOPSIS @@ -25,23 +25,24 @@ our $VERSION = '0.05'; $re = wc2re 'a{b?,c}*' => 'unix'; # Do it Unix style. $re = wc2re 'a?,b*' => 'win32'; # Do it Windows style. $re = wc2re '*{x,y}?' => 'jokers'; # Process the jokers & escape the rest. + $re = wc2re '%a_c%' => 'sql'; # Turn SQL wildcards into regexps. =head1 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 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. +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 shell jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards. Backspace (C<\>) is used as an escape character. Wrappers are provided to mimic the behaviour of Windows and Unix shells. =head1 VARIABLES These variables control if the wildcards jokers and brackets must capture their match. They can be globally set by writing in your program $Regexp::Wildcards::CaptureSingle = 1; - # From then, the '?' joker is capturing + # From then, "exactly one" wildcards are capturing or can be locally specified via C { - local $Regexp::Wildcards::CaptureAny = 1; - # In this block, the '?' joker is capturing. + local $Regexp::Wildcards::CaptureSingle = 1; + # In this block, "exactly one" wildcards are capturing. ... } # Back to the situation from before the block @@ -50,27 +51,49 @@ This section describes also how those elements are translated by the L -When this variable is true, each occurence of the unescaped C joker is made capturing in the resulting regexp (they are be replaced by C<(.)>). Otherwise, they are just replaced by C<.>. Default is the latter. +When this variable is true, each occurence of unescaped I<"exactly one"> wildcards (i.e. C jokers or C<_> for SQL wildcards) are made capturing in the resulting regexp (they are be replaced by C<(.)>). Otherwise, they are just replaced by C<.>. Default is the latter. + For jokers : 'a???b\\??' is translated to 'a(.)(.)(.)b\\?(.)' if $CaptureSingle is true 'a...b\\?.' otherwise (default) + For SQL wildcards : + 'a___b\\__' is translated to 'a(.)(.)(.)b\\_(.)' if $CaptureSingle is true + 'a...b\\_.' otherwise (default) + =cut our $CaptureSingle = 0; +sub capture_single { + return $CaptureSingle ? '(.)' + : '.'; +} + =head2 C<$CaptureAny> -By default this variable is false, and successions of unescaped C<*> jokers are replaced by B single C<.*>. When it evalutes to true, those sequences of C<*> are made into B capture, which is greedy (C<(.*)>) for C<$CaptureAny E 0> and otherwise non-greedy (C<(.*?)>). +By default this variable is false, and successions of unescaped I<"any"> wildcards (i.e. C<*> jokers or C<%> for SQL wildcards) are replaced by B single C<.*>. When it evalutes to true, those sequences of I<"any"> wildcards are made into B capture, which is greedy (C<(.*)>) for C<$CaptureAny E 0> and otherwise non-greedy (C<(.*?)>). + For jokers : 'a***b\\**' is translated to 'a.*b\\*.*' if $CaptureAny is false (default) 'a(.*)b\\*(.*)' if $CaptureAny > 0 'a(.*?)b\\*(.*?)' otherwise + For SQL wildcards : + 'a%%%b\\%%' is translated to 'a.*b\\%.*' if $CaptureAny is false (default) + 'a(.*)b\\%(.*)' if $CaptureAny > 0 + 'a(.*?)b\\%(.*?)' otherwise + =cut our $CaptureAny = 0; +sub capture_any { + return $CaptureAny ? (($CaptureAny > 0) ? '(.*)' + : '(.*?)') + : '.*'; +} + =head2 C<$CaptureBrackets> If this variable is set to true, valid brackets constructs are made into C<( | )> captures, and otherwise they are replaced by non-capturing alternations (C<(?: | >)), which is the default. @@ -82,11 +105,16 @@ If this variable is set to true, valid brackets constructs are made into C<( | ) our $CaptureBrackets = 0; +sub capture_brackets { + return $CaptureBrackets ? '(' + : '(?:'; +} + =head1 FUNCTIONS =head2 C -This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression where the jokers C and C<*> have been translated into their regexp equivalents (see L for more details). All other unprotected regexp metacharacters are escaped. +This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression where the jokers C (I<"exactly one">) and C<*> (I<"any">) have been translated into their regexp equivalents (see L for more details). All other unprotected regexp metacharacters are escaped. # Everything is escaped. print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}'; @@ -99,9 +127,21 @@ sub wc2re_jokers { return do_jokers($wc); } +=head2 C + +Similar to the precedent, but for the SQL wildcards C<_> (I<"exactly one">) and C<%> (I<"any">). All other unprotected regexp metacharacters are escaped. + +=cut + +sub wc2re_sql { + my ($wc) = @_; + $wc =~ s/(? -Similar to the precedent, but this one conforms to standard Unix shell wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns jokers into their regexp equivalents (see L), and changes bracketed blocks into (possibly capturing) alternations as described in L. If brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped. +This function conforms to standard Unix shell wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns C and C<*> jokers into their regexp equivalents (see L), and changes bracketed blocks into (possibly capturing) alternations as described in L. If brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped. # This is a valid bracket expression, and is completely translated. print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)'; @@ -125,7 +165,7 @@ sub wc2re_unix { =head2 C -This one works just like the two before, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets are escaped), but you can provide a comma-separated list of items. +This one works just like the one before, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets are escaped), but you can provide a comma-separated list of items. # All the brackets are escaped, and commas are seen as list delimiters. print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})'; @@ -138,7 +178,7 @@ sub wc2re_win32 { $wc =~ s/(?, C<'win32'>, C<'jokers'> +=item C<'unix'>, C<'win32'>, C<'jokers'>, C<'sql'> -For one of those raw rule names, C simply maps to C, C and C respectively. +For one of those raw rule names, C simply maps to C, C, C and C respectively. =item C<$^O> @@ -172,6 +212,7 @@ If the type is undefined or not supported, it defaults to C<'unix'>. my %types = ( 'jokers' => \&wc2re_jokers, + 'sql' => \&wc2re_sql, 'unix' => \&wc2re_unix, map { lc $_ => \&wc2re_win32 } qw/win32 dos os2 MSWin32 cygwin/ ); @@ -186,15 +227,17 @@ sub wc2re { =head1 EXPORT -These four functions are exported only on request : C, C, C and C. The variables are not exported. +These five functions are exported only on request : C, C, C, C and C. The variables are not exported. =cut use base qw/Exporter/; our @EXPORT = (); -our @EXPORT_OK = ('wc2re', map { 'wc2re_' . $_ } keys %types); -our @EXPORT_FAIL = qw/extract do_jokers do_commas do_brackets do_bracketed/; +our @EXPORT_OK = ('wc2re', map { 'wc2re_'.$_ } keys %types); +our @EXPORT_FAIL = qw/extract/, + (map { 'do_'.$_ } qw/jokers sql commas brackets bracketed/), + (map { 'capture_'.$_ } qw/single any brackets/); our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); =head1 DEPENDENCIES @@ -249,15 +292,27 @@ sub do_jokers { # escape an odd number of \ that doesn't protect a regexp/wildcard special char s/(? 0) ? '(.*)' : '(.*?)') - : '.*'; + $s = capture_any; s/(?