regexprs.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. ```nim
  40. r"\S" # matches any character that is not whitespace
  41. ```
  42. A regular expression is a pattern that is matched against a subject string
  43. from left to right. Most characters stand for themselves in a pattern, and
  44. match the corresponding characters in the subject. As a trivial example,
  45. the pattern:
  46. The quick brown fox
  47. matches a portion of a subject string that is identical to itself.
  48. The power of regular expressions comes from the ability to include
  49. alternatives and repetitions in the pattern. These are encoded in
  50. the pattern by the use of metacharacters, which do not stand for
  51. themselves but instead are interpreted in some special way.
  52. There are two different sets of metacharacters: those that are recognized
  53. anywhere in the pattern except within square brackets, and those that are
  54. recognized in square brackets. Outside square brackets, the metacharacters
  55. are as follows:
  56. ============== ============================================================
  57. meta character meaning
  58. ============== ============================================================
  59. ``\`` general escape character with several uses
  60. ``^`` assert start of string (or line, in multiline mode)
  61. ``$`` assert end of string (or line, in multiline mode)
  62. ``.`` match any character except newline (by default)
  63. ``[`` start character class definition
  64. ``|`` start of alternative branch
  65. ``(`` start subpattern
  66. ``)`` end subpattern
  67. ``{`` start min/max quantifier
  68. ``?`` extends the meaning of ``(``
  69. | also 0 or 1 quantifier (equal to ``{0,1}``)
  70. | also quantifier minimizer
  71. ``*`` 0 or more quantifier (equal to ``{0,}``)
  72. ``+`` 1 or more quantifier (equal to ``{1,}``)
  73. | also "possessive quantifier"
  74. ============== ============================================================
  75. Part of a pattern that is in square brackets is called a "character class".
  76. In a character class the only metacharacters are:
  77. ============== ============================================================
  78. meta character meaning
  79. ============== ============================================================
  80. ``\`` general escape character
  81. ``^`` negate the class, but only if the first character
  82. ``-`` indicates character range
  83. ``[`` POSIX character class (only if followed by POSIX syntax)
  84. ``]`` terminates the character class
  85. ============== ============================================================
  86. The following sections describe the use of each of the metacharacters.
  87. Backslash
  88. ---------
  89. The `backslash`:idx: character has several uses. Firstly, if it is followed
  90. by a non-alphanumeric character, it takes away any special meaning that
  91. character may have. This use of backslash as an escape character applies
  92. both inside and outside character classes.
  93. For example, if you want to match a ``*`` character, you write ``\*`` in
  94. the pattern. This escaping action applies whether or not the following
  95. character would otherwise be interpreted as a metacharacter, so it is always
  96. safe to precede a non-alphanumeric with backslash to specify that it stands
  97. for itself. In particular, if you want to match a backslash, you write ``\\``.
  98. Non-printing characters
  99. -----------------------
  100. A second use of backslash provides a way of encoding non-printing characters
  101. in patterns in a visible manner. There is no restriction on the appearance of
  102. non-printing characters, apart from the binary zero that terminates a pattern,
  103. but when a pattern is being prepared by text editing, it is usually easier to
  104. use one of the following escape sequences than the binary character it
  105. represents:
  106. ============== ============================================================
  107. character meaning
  108. ============== ============================================================
  109. ``\a`` alarm, that is, the BEL character (hex 07)
  110. ``\e`` escape (hex 1B)
  111. ``\f`` formfeed (hex 0C)
  112. ``\n`` newline (hex 0A)
  113. ``\r`` carriage return (hex 0D)
  114. ``\t`` tab (hex 09)
  115. ``\ddd`` character with octal code ddd, or backreference
  116. ``\xhh`` character with hex code hh
  117. ============== ============================================================
  118. After ``\x``, from zero to two hexadecimal digits are read (letters can be in
  119. upper or lower case). In UTF-8 mode, any number of hexadecimal digits may
  120. appear between ``\x{`` and ``}``, but the value of the character code must be
  121. less than 2^31 (that is, the maximum hexadecimal value is 7FFFFFFF). If
  122. characters other than hexadecimal digits appear between ``\x{`` and ``}``, or
  123. if there is no terminating ``}``, this form of escape is not recognized.
  124. Instead, the initial ``\x`` will be interpreted as a basic hexadecimal escape,
  125. with no following digits, giving a character whose value is zero.
  126. After ``\0`` up to two further octal digits are read. In both cases, if there
  127. are fewer than two digits, just those that are present are used. Thus the
  128. sequence ``\0\x\07`` specifies two binary zeros followed by a BEL character
  129. (code value 7). Make sure you supply two digits after the initial zero if
  130. the pattern character that follows is itself an octal digit.
  131. The handling of a backslash followed by a digit other than 0 is complicated.
  132. Outside a character class, PCRE reads it and any following digits as a
  133. decimal number. If the number is less than 10, or if there have been at least
  134. that many previous capturing left parentheses in the expression, the entire
  135. sequence is taken as a back reference. A description of how this works is
  136. given later, following the discussion of parenthesized subpatterns.
  137. Inside a character class, or if the decimal number is greater than 9 and
  138. there have not been that many capturing subpatterns, PCRE re-reads up to
  139. three octal digits following the backslash, and generates a single byte
  140. from the least significant 8 bits of the value. Any subsequent digits stand
  141. for themselves. For example:
  142. ============== ============================================================
  143. example meaning
  144. ============== ============================================================
  145. ``\040`` is another way of writing a space
  146. ``\40`` is the same, provided there are fewer than 40 previous
  147. capturing subpatterns
  148. ``\7`` is always a back reference
  149. ``\11`` might be a back reference, or another way of writing a tab
  150. ``\011`` is always a tab
  151. ``\0113`` is a tab followed by the character "3"
  152. ``\113`` might be a back reference, otherwise the character with
  153. octal code 113
  154. ``\377`` might be a back reference, otherwise the byte consisting
  155. entirely of 1 bits
  156. ``\81`` is either a back reference, or a binary zero followed by
  157. the two characters "8" and "1"
  158. ============== ============================================================
  159. Note that octal values of 100 or greater must not be introduced by a leading
  160. zero, because no more than three octal digits are ever read.
  161. All the sequences that define a single byte value or a single UTF-8 character
  162. (in UTF-8 mode) can be used both inside and outside character classes. In
  163. addition, inside a character class, the sequence ``\b`` is interpreted as the
  164. backspace character (hex 08), and the sequence ``\X`` is interpreted as the
  165. character "X". Outside a character class, these sequences have different
  166. meanings (see below).
  167. Generic character types
  168. -----------------------
  169. The third use of backslash is for specifying `generic character types`:idx:.
  170. The following are always recognized:
  171. ============== ============================================================
  172. character type meaning
  173. ============== ============================================================
  174. ``\d`` any decimal digit
  175. ``\D`` any character that is not a decimal digit
  176. ``\s`` any whitespace character
  177. ``\S`` any character that is not a whitespace character
  178. ``\w`` any "word" character
  179. ``\W`` any "non-word" character
  180. ============== ============================================================
  181. Each pair of escape sequences partitions the complete set of characters into
  182. two disjoint sets. Any given character matches one, and only one, of each pair.
  183. These character type sequences can appear both inside and outside character
  184. classes. They each match one character of the appropriate type. If the
  185. current matching point is at the end of the subject string, all of them fail,
  186. since there is no character to match.
  187. For compatibility with Perl, ``\s`` does not match the VT character (code 11).
  188. This makes it different from the POSIX "space" class. The ``\s`` characters
  189. are HT (9), LF (10), FF (12), CR (13), and space (32).
  190. A "word" character is an underscore or any character less than 256 that is
  191. a letter or digit. The definition of letters and digits is controlled by
  192. PCRE's low-valued character tables, and may vary if locale-specific matching
  193. is taking place (see "Locale support" in the pcreapi page). For example,
  194. in the "fr_FR" (French) locale, some character codes greater than 128 are
  195. used for accented letters, and these are matched by ``\w``.
  196. In UTF-8 mode, characters with values greater than 128 never match ``\d``,
  197. ``\s``, or ``\w``, and always match ``\D``, ``\S``, and ``\W``. This is true
  198. even when Unicode character property support is available.
  199. Simple assertions
  200. -----------------
  201. The fourth use of backslash is for certain `simple assertions`:idx:. An
  202. assertion specifies a condition that has to be met at a particular point in
  203. a match, without consuming any characters from the subject string. The use of
  204. subpatterns for more complicated assertions is described below. The
  205. backslashed assertions are:
  206. ============== ============================================================
  207. assertion meaning
  208. ============== ============================================================
  209. ``\b`` matches at a word boundary
  210. ``\B`` matches when not at a word boundary
  211. ``\A`` matches at start of subject
  212. ``\Z`` matches at end of subject or before newline at end
  213. ``\z`` matches at end of subject
  214. ``\G`` matches at first matching position in subject
  215. ============== ============================================================
  216. These assertions may not appear in character classes (but note that ``\b``
  217. has a different meaning, namely the backspace character, inside a character
  218. class).
  219. A word boundary is a position in the subject string where the current
  220. character and the previous character do not both match ``\w`` or ``\W`` (i.e.
  221. one matches ``\w`` and the other matches ``\W``), or the start or end of the
  222. string if the first or last character matches ``\w``, respectively.
  223. The ``\A``, ``\Z``, and ``\z`` assertions differ from the traditional
  224. circumflex and dollar in that they only ever match at the very start and
  225. end of the subject string, whatever options are set.
  226. The difference between ``\Z`` and ``\z`` is that ``\Z`` matches before
  227. a newline that is the last character of the string as well as at the end
  228. of the string, whereas ``\z`` matches only at the end.