README 9.6 KB

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