regexprs.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. Licence of the PCRE library
  2. ===========================
  3. PCRE is a library of functions to support regular expressions whose
  4. syntax and semantics are as close as possible to those of the Perl 5
  5. language.
  6. | Written by Philip Hazel
  7. | Copyright (c) 1997-2005 University of Cambridge
  8. ----------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. Regular expression syntax and semantics
  31. =======================================
  32. As the regular expressions supported by this module are enormous,
  33. the reader is referred to http://perldoc.perl.org/perlre.html for the
  34. full documentation of Perl's regular expressions.
  35. Because the backslash ``\`` is a meta character both in the Nim
  36. programming language and in regular expressions, it is strongly
  37. recommended that one uses the *raw* strings of Nim, so that
  38. backslashes are interpreted by the regular expression engine::
  39. r"\S" # matches any character that is not whitespace
  40. A regular expression is a pattern that is matched against a subject string
  41. from left to right. Most characters stand for themselves in a pattern, and
  42. match the corresponding characters in the subject. As a trivial example,
  43. the pattern::
  44. The quick brown fox
  45. matches a portion of a subject string that is identical to itself.
  46. The power of regular expressions comes from the ability to include
  47. alternatives and repetitions in the pattern. These are encoded in
  48. the pattern by the use of metacharacters, which do not stand for
  49. themselves but instead are interpreted in some special way.
  50. There are two different sets of metacharacters: those that are recognized
  51. anywhere in the pattern except within square brackets, and those that are
  52. recognized in square brackets. Outside square brackets, the metacharacters
  53. are as follows:
  54. ============== ============================================================
  55. meta character meaning
  56. ============== ============================================================
  57. ``\`` general escape character with several uses
  58. ``^`` assert start of string (or line, in multiline mode)
  59. ``$`` assert end of string (or line, in multiline mode)
  60. ``.`` match any character except newline (by default)
  61. ``[`` start character class definition
  62. ``|`` start of alternative branch
  63. ``(`` start subpattern
  64. ``)`` end subpattern
  65. ``?`` extends the meaning of ``(``
  66. also 0 or 1 quantifier
  67. also quantifier minimizer
  68. ``*`` 0 or more quantifier
  69. ``+`` 1 or more quantifier
  70. also "possessive quantifier"
  71. ``{`` start min/max quantifier
  72. ============== ============================================================
  73. Part of a pattern that is in square brackets is called a "character class".
  74. In a character class the only metacharacters are:
  75. ============== ============================================================
  76. meta character meaning
  77. ============== ============================================================
  78. ``\`` general escape character
  79. ``^`` negate the class, but only if the first character
  80. ``-`` indicates character range
  81. ``[`` POSIX character class (only if followed by POSIX syntax)
  82. ``]`` terminates the character class
  83. ============== ============================================================
  84. The following sections describe the use of each of the metacharacters.
  85. Backslash
  86. ---------
  87. The `backslash`:idx: character has several uses. Firstly, if it is followed
  88. by a non-alphanumeric character, it takes away any special meaning that
  89. character may have. This use of backslash as an escape character applies
  90. both inside and outside character classes.
  91. For example, if you want to match a ``*`` character, you write ``\*`` in
  92. the pattern. This escaping action applies whether or not the following
  93. character would otherwise be interpreted as a metacharacter, so it is always
  94. safe to precede a non-alphanumeric with backslash to specify that it stands
  95. for itself. In particular, if you want to match a backslash, you write ``\\``.
  96. Non-printing characters
  97. -----------------------
  98. A second use of backslash provides a way of encoding non-printing characters
  99. in patterns in a visible manner. There is no restriction on the appearance of
  100. non-printing characters, apart from the binary zero that terminates a pattern,
  101. but when a pattern is being prepared by text editing, it is usually easier to
  102. use one of the following escape sequences than the binary character it
  103. represents::
  104. ============== ============================================================
  105. character meaning
  106. ============== ============================================================
  107. ``\a`` alarm, that is, the BEL character (hex 07)
  108. ``\e`` escape (hex 1B)
  109. ``\f`` formfeed (hex 0C)
  110. ``\n`` newline (hex 0A)
  111. ``\r`` carriage return (hex 0D)
  112. ``\t`` tab (hex 09)
  113. ``\ddd`` character with octal code ddd, or backreference
  114. ``\xhh`` character with hex code hh
  115. ============== ============================================================
  116. After ``\x``, from zero to two hexadecimal digits are read (letters can be in
  117. upper or lower case). In UTF-8 mode, any number of hexadecimal digits may
  118. appear between ``\x{`` and ``}``, but the value of the character code must be
  119. less than 2**31 (that is, the maximum hexadecimal value is 7FFFFFFF). If
  120. characters other than hexadecimal digits appear between ``\x{`` and ``}``, or
  121. if there is no terminating ``}``, this form of escape is not recognized.
  122. Instead, the initial ``\x`` will be interpreted as a basic hexadecimal escape,
  123. with no following digits, giving a character whose value is zero.
  124. After ``\0`` up to two further octal digits are read. In both cases, if there
  125. are fewer than two digits, just those that are present are used. Thus the
  126. sequence ``\0\x\07`` specifies two binary zeros followed by a BEL character
  127. (code value 7). Make sure you supply two digits after the initial zero if
  128. the pattern character that follows is itself an octal digit.
  129. The handling of a backslash followed by a digit other than 0 is complicated.
  130. Outside a character class, PCRE reads it and any following digits as a
  131. decimal number. If the number is less than 10, or if there have been at least
  132. that many previous capturing left parentheses in the expression, the entire
  133. sequence is taken as a back reference. A description of how this works is
  134. given later, following the discussion of parenthesized subpatterns.
  135. Inside a character class, or if the decimal number is greater than 9 and
  136. there have not been that many capturing subpatterns, PCRE re-reads up to
  137. three octal digits following the backslash, and generates a single byte
  138. from the least significant 8 bits of the value. Any subsequent digits stand
  139. for themselves. For example:
  140. ============== ============================================================
  141. example meaning
  142. ============== ============================================================
  143. ``\040`` is another way of writing a space
  144. ``\40`` is the same, provided there are fewer than 40 previous
  145. capturing subpatterns
  146. ``\7`` is always a back reference
  147. ``\11`` might be a back reference, or another way of writing a tab
  148. ``\011`` is always a tab
  149. ``\0113`` is a tab followed by the character "3"
  150. ``\113`` might be a back reference, otherwise the character with
  151. octal code 113
  152. ``\377`` might be a back reference, otherwise the byte consisting
  153. entirely of 1 bits
  154. ``\81`` is either a back reference, or a binary zero followed by
  155. the two characters "8" and "1"
  156. ============== ============================================================
  157. Note that octal values of 100 or greater must not be introduced by a leading
  158. zero, because no more than three octal digits are ever read.
  159. All the sequences that define a single byte value or a single UTF-8 character
  160. (in UTF-8 mode) can be used both inside and outside character classes. In
  161. addition, inside a character class, the sequence ``\b`` is interpreted as the
  162. backspace character (hex 08), and the sequence ``\X`` is interpreted as the
  163. character "X". Outside a character class, these sequences have different
  164. meanings (see below).
  165. Generic character types
  166. -----------------------
  167. The third use of backslash is for specifying `generic character types`:idx:.
  168. The following are always recognized:
  169. ============== ============================================================
  170. character type meaning
  171. ============== ============================================================
  172. ``\d`` any decimal digit
  173. ``\D`` any character that is not a decimal digit
  174. ``\s`` any whitespace character
  175. ``\S`` any character that is not a whitespace character
  176. ``\w`` any "word" character
  177. ``\W`` any "non-word" character
  178. ============== ============================================================
  179. Each pair of escape sequences partitions the complete set of characters into
  180. two disjoint sets. Any given character matches one, and only one, of each pair.
  181. These character type sequences can appear both inside and outside character
  182. classes. They each match one character of the appropriate type. If the
  183. current matching point is at the end of the subject string, all of them fail,
  184. since there is no character to match.
  185. For compatibility with Perl, ``\s`` does not match the VT character (code 11).
  186. This makes it different from the the POSIX "space" class. The ``\s`` characters
  187. are HT (9), LF (10), FF (12), CR (13), and space (32).
  188. A "word" character is an underscore or any character less than 256 that is
  189. a letter or digit. The definition of letters and digits is controlled by
  190. PCRE's low-valued character tables, and may vary if locale-specific matching
  191. is taking place (see "Locale support" in the pcreapi page). For example,
  192. in the "fr_FR" (French) locale, some character codes greater than 128 are
  193. used for accented letters, and these are matched by ``\w``.
  194. In UTF-8 mode, characters with values greater than 128 never match ``\d``,
  195. ``\s``, or ``\w``, and always match ``\D``, ``\S``, and ``\W``. This is true
  196. even when Unicode character property support is available.
  197. Simple assertions
  198. -----------------
  199. The fourth use of backslash is for certain `simple assertions`:idx:. An
  200. assertion specifies a condition that has to be met at a particular point in
  201. a match, without consuming any characters from the subject string. The use of
  202. subpatterns for more complicated assertions is described below. The
  203. backslashed assertions are::
  204. ============== ============================================================
  205. assertion meaning
  206. ============== ============================================================
  207. ``\b`` matches at a word boundary
  208. ``\B`` matches when not at a word boundary
  209. ``\A`` matches at start of subject
  210. ``\Z`` matches at end of subject or before newline at end
  211. ``\z`` matches at end of subject
  212. ``\G`` matches at first matching position in subject
  213. ============== ============================================================
  214. These assertions may not appear in character classes (but note that ``\b``
  215. has a different meaning, namely the backspace character, inside a character
  216. class).
  217. A word boundary is a position in the subject string where the current
  218. character and the previous character do not both match ``\w`` or ``\W`` (i.e.
  219. one matches ``\w`` and the other matches ``\W``), or the start or end of the
  220. string if the first or last character matches ``\w``, respectively.
  221. The ``\A``, ``\Z``, and ``\z`` assertions differ from the traditional
  222. circumflex and dollar in that they only ever match at the very start and
  223. end of the subject string, whatever options are set.
  224. The difference between ``\Z`` and ``\z`` is that ``\Z`` matches before
  225. a newline that is the last character of the string as well as at the end
  226. of the string, whereas ``\z`` matches only at the end.