Debian Codegraph @

vendor/onigmo-source/doc/RE

raw
1 Onigmo (Oniguruma-mod) Regular Expressions Version 6.1.0 2016/12/25
2
3 syntax: ONIG_SYNTAX_RUBY (default)
4
5
6 1. Syntax elements
7
8 \ escape (enable or disable meta character)
9 | alternation
10 (...) group
11 [...] character class
12
13
14 2. Characters
15
16 \t horizontal tab (0x09)
17 \v vertical tab (0x0B)
18 \n newline (line feed) (0x0A)
19 \r carriage return (0x0D)
20 \b backspace (0x08)
21 \f form feed (0x0C)
22 \a bell (0x07)
23 \e escape (0x1B)
24 \nnn octal char (encoded byte value)
25 \xHH hexadecimal char (encoded byte value)
26 \x{7HHHHHHH} wide hexadecimal char (character code point value)
27 \uHHHH wide hexadecimal char (character code point value)
28 \cx control char (character code point value)
29 \C-x control char (character code point value)
30 \M-x meta (x|0x80) (character code point value)
31 \M-\C-x meta control char (character code point value)
32
33 (* \b as backspace is effective in character class only)
34
35 * ONIG_SYNTAX_PERL: \o{nnn} (octal char) can be also used.
36
37
38 3. Character types
39
40 . any character (except newline)
41
42 \w word character
43
44 Not Unicode:
45 alphanumeric and "_".
46
47 Unicode:
48 General_Category -- (Letter|Mark|Number|Connector_Punctuation)
49
50 It depends on ONIG_OPTION_ASCII_RANGE option that non-ASCII char
51 includes or not.
52
53 \W non-word char
54
55 \s whitespace char
56
57 Not Unicode:
58 \t, \n, \v, \f, \r, \x20
59
60 Unicode:
61 0009, 000A, 000B, 000C, 000D, 0085(NEL),
62 General_Category -- Line_Separator
63 -- Paragraph_Separator
64 -- Space_Separator
65
66 It depends on ONIG_OPTION_ASCII_RANGE option that non-ASCII char
67 includes or not.
68
69 \S non-whitespace char
70
71 \d decimal digit char
72
73 Unicode: General_Category -- Decimal_Number
74
75 It depends on ONIG_OPTION_ASCII_RANGE option that non-ASCII char
76 includes or not.
77
78 \D non-decimal-digit char
79
80 \h hexadecimal-digit char [0-9a-fA-F]
81
82 \H non-hexadecimal-digit char
83
84
85 Character Property
86
87 * \p{property-name}
88 * \p{^property-name} (negative)
89 * \P{property-name} (negative)
90
91 property-name:
92
93 + works on all encodings
94 Alnum, Alpha, Blank, Cntrl, Digit, Graph, Lower,
95 Print, Punct, Space, Upper, XDigit, Word, ASCII
96
97 + works on EUC_JP, Shift_JIS, CP932
98 Hiragana, Katakana, Han, Latin, Greek, Cyrillic
99
100 + works on UTF-8, UTF-16, UTF-32
101 see UnicodeProps.txt
102
103 \p{Punct} works slightly different on Unicode encodings and the other
104 encodings. It matches the nine characters "$+<=>^`|~" on non-Unicode
105 encodings (which is the same as [[:punct:]]), but not on Unicode encodings.
106 \p{XPosixPunct} matches the nine characters on Unicode encodings.
107
108
109 \R Linebreak
110
111 Unicode:
112 (?>\x0D\x0A|[\x0A-\x0D\x{85}\x{2028}\x{2029}])
113
114 Not Unicode:
115 (?>\x0D\x0A|[\x0A-\x0D])
116
117 \X Extended Grapheme cluster
118
119 Unicode:
120 See: Unicode Standard Annex #29 UNICODE TEXT SEGMENTATION
121 http://unicode.org/reports/tr29/
122
123 Not Unicode:
124 (?>\x0D\x0A|(?m:.))
125
126
127
128 4. Quantifier
129
130 greedy
131
132 ? 1 or 0 times
133 * 0 or more times
134 + 1 or more times
135 {n,m} at least n but no more than m times
136 {n,} at least n times
137 {,n} at least 0 but no more than n times ({0,n})
138 {n} n times
139
140 reluctant
141
142 ?? 1 or 0 times
143 *? 0 or more times
144 +? 1 or more times
145 {n,m}? at least n but not more than m times
146 {n,}? at least n times
147 {,n}? at least 0 but not more than n times (== {0,n}?)
148
149 possessive (greedy and does not backtrack once match)
150
151 ?+ 1 or 0 times
152 *+ 0 or more times
153 ++ 1 or more times
154
155 ({n,m}+, {n,}+, {n}+ are possessive op. in ONIG_SYNTAX_JAVA and
156 ONIG_SYNTAX_PERL only)
157
158 ex. /a*+/ === /(?>a*)/
159
160
161 5. Anchors
162
163 ^ beginning of the line
164 $ end of the line
165 \b word boundary
166 \B non-word boundary
167 \A beginning of string
168 \Z end of string, or before newline at the end
169 \z end of string
170 \G where the current search attempt begins
171
172
173 6. Character class
174
175 ^... negative class (lowest precedence)
176 x-y range from x to y
177 [...] set (character class in character class)
178 ..&&.. intersection (low precedence, only higher than ^)
179
180 ex. [a-w&&[^c-g]z] ==> ([a-w] AND ([^c-g] OR z)) ==> [abh-w]
181
182 * If you want to use '[', '-', or ']' as a normal character
183 in character class, you should escape them with '\'.
184
185
186 POSIX bracket ([:xxxxx:], negate [:^xxxxx:])
187
188 Not Unicode Case:
189
190 alnum alphabet or digit char
191 alpha alphabet
192 ascii code value: [0 - 127]
193 blank \t, \x20
194 cntrl
195 digit 0-9
196 graph \x21-\x7E and all of multibyte encoded characters
197 lower
198 print \x20-\x7E and all of multibyte encoded characters
199 punct
200 space \t, \n, \v, \f, \r, \x20
201 upper
202 xdigit 0-9, a-f, A-F
203 word alphanumeric, "_" and multibyte characters
204
205
206 Unicode Case:
207
208 alnum Letter | Mark | Decimal_Number
209 alpha Letter | Mark
210 ascii 0000 - 007F
211 blank Space_Separator | 0009
212 cntrl Control | Format | Unassigned | Private_Use | Surrogate
213 digit Decimal_Number
214 graph [[:^space:]] && ^Control && ^Unassigned && ^Surrogate
215 lower Lowercase_Letter
216 print [[:graph:]] | Space_Separator
217 punct Connector_Punctuation | Dash_Punctuation | Close_Punctuation |
218 Final_Punctuation | Initial_Punctuation | Other_Punctuation |
219 Open_Punctuation | 0024 | 002B | 003C | 003D | 003E | 005E |
220 0060 | 007C | 007E
221 space Space_Separator | Line_Separator | Paragraph_Separator |
222 0009 | 000A | 000B | 000C | 000D | 0085
223 upper Uppercase_Letter
224 xdigit 0030 - 0039 | 0041 - 0046 | 0061 - 0066
225 (0-9, a-f, A-F)
226 word Letter | Mark | Decimal_Number | Connector_Punctuation
227
228
229 It depends on ONIG_OPTION_ASCII_RANGE option and
230 ONIG_OPTION_POSIX_BRACKET_ALL_RANGE option that POSIX brackets
231 match non-ASCII char or not.
232
233
234
235 7. Extended groups
236
237 (?#...) comment
238
239 (?imxdau-imx) option on/off
240 i: ignore case
241 m: multi-line (dot (.) also matches newline)
242 x: extended form
243
244 character set option (character range option)
245 d: Default (compatible with Ruby 1.9.3)
246 \w, \d and \s doesn't match non-ASCII characters.
247 \b, \B and POSIX brackets use the each encoding's
248 rules.
249 a: ASCII
250 ONIG_OPTION_ASCII_RANGE option is turned on.
251 \w, \d, \s and POSIX brackets doesn't match
252 non-ASCII characters.
253 \b and \B use the ASCII rules.
254 u: Unicode
255 ONIG_OPTION_ASCII_RANGE option is turned off.
256 \w (\W), \d (\D), \s (\S), \b (\B) and POSIX
257 brackets use the each encoding's rules.
258
259 (?imxdau-imx:subexp)
260 option on/off for subexp
261
262 (?:subexp) non-capturing group
263 (subexp) capturing group
264
265 (?=subexp) look-ahead
266 (?!subexp) negative look-ahead
267 (?<=subexp) look-behind
268 (?<!subexp) negative look-behind
269
270 Subexp of look-behind must be fixed-width.
271 But top-level alternatives can be of various lengths.
272 ex. (?<=a|bc) is OK. (?<=aaa(?:b|cd)) is not allowed.
273
274 In negative look-behind, capturing group isn't allowed,
275 but non-capturing group (?:) is allowed.
276
277 \K keep
278 Another expression of look-behind. Keep the stuff left
279 of the \K, don't include it in the result.
280
281 (?>subexp) atomic group
282 no backtracks in subexp.
283
284 (?<name>subexp), (?'name'subexp)
285 define named group
286 (Each character of the name must be a word character.)
287
288 Not only a name but a number is assigned like a capturing
289 group.
290
291 Assigning the same name to two or more subexps is allowed.
292
293 (?(cond)yes-subexp), (?(cond)yes-subexp|no-subexp)
294 conditional expression
295 Matches yes-subexp if (cond) yields a true value, matches
296 no-subexp otherwise.
297 Following (cond) can be used:
298
299 (n) (n >= 1)
300 Checks if the numbered capturing group has matched
301 something.
302
303 (<name>), ('name')
304 Checks if a group with the given name has matched
305 something.
306
307 BUG: If the name is defined more than once, the
308 left-most group is checked, but it should be the
309 same as \k<name>.
310
311 (?~subexp) absent operator (experimental)
312 Matches any string which doesn't contain any string which
313 matches subexp.
314 Similar to (?:(?!subexp).)*, but easy to write.
315
316 E.g.:
317 (?~abc) matches: "", "ab", "aab", "ccdd", etc.
318 It doesn't match: "abc", "aabc", "ccabcdd", etc.
319
320 \/\*(?~\*\/)\*\/ matches C style comments:
321 "/**/", "/* foobar */", etc.
322
323 \A\/\*(?~\*\/)\*\/\z doesn't match "/**/ */".
324 This is different from \A\/\*.*?\*\/\z which uses a
325 reluctant quantifier (.*?).
326
327 Unlike (?:(?!abc).)*c, (?~abc)c matches "abc", because
328 (?~abc) matches "ab".
329
330 (?~) never matches.
331
332 Theoretical backgrounds are discussed in Tanaka Akira's
333 paper and slide (both Japanese):
334
335 * Absent Operator for Regular Expression
336 https://staff.aist.go.jp/tanaka-akira/pub/prosym49-akr-paper.pdf
337 * 正規表現における非包含オペレータの提案
338 https://staff.aist.go.jp/tanaka-akira/pub/prosym49-akr-presen.pdf
339
340
341 8. Backreferences
342
343 When we say "backreference a group," it actually means, "re-match the same
344 text matched by the subexp in that group."
345
346 \n \k<n> \k'n' (n >= 1) backreference the nth group in the regexp
347 \k<-n> \k'-n' (n >= 1) backreference the nth group counting
348 backwards from the referring position
349 \k<name> \k'name' backreference a group with the specified name
350
351 When backreferencing with a name that is assigned to more than one groups,
352 the last group with the name is checked first, if not matched then the
353 previous one with the name, and so on, until there is a match.
354
355 * Backreference by number is forbidden if any named group is defined and
356 ONIG_OPTION_CAPTURE_GROUP is not set.
357
358 * ONIG_SYNTAX_PERL: \g{n}, \g{-n} and \g{name} can also be used.
359 If a name is defined more than once in Perl syntax, only the left-most
360 group is checked.
361
362
363 backreference with recursion level
364
365 (n >= 1, level >= 0)
366
367 \k<n+level> \k'n+level'
368 \k<n-level> \k'n-level'
369 \k<-n+level> \k'-n+level'
370 \k<-n-level> \k'-n-level'
371
372 \k<name+level> \k'name+level'
373 \k<name-level> \k'name-level'
374
375 Destine a group on the recursion level relative to the referring position.
376
377 ex 1.
378
379 /\A(?<a>|.|(?:(?<b>.)\g<a>\k<b>))\z/.match("reee")
380 /\A(?<a>|.|(?:(?<b>.)\g<a>\k<b+0>))\z/.match("reer")
381
382 \k<b+0> refers to the (?<b>.) on the same recursion level with it.
383
384 ex 2.
385
386 r = Regexp.compile(<<'__REGEXP__'.strip, Regexp::EXTENDED)
387 (?<element> \g<stag> \g<content>* \g<etag> ){0}
388 (?<stag> < \g<name> \s* > ){0}
389 (?<name> [a-zA-Z_:]+ ){0}
390 (?<content> [^<&]+ (\g<element> | [^<&]+)* ){0}
391 (?<etag> </ \k<name+1> >){0}
392 \g<element>
393 __REGEXP__
394
395 p r.match("<foo>f<bar>bbb</bar>f</foo>").captures
396
397
398 9. Subexp calls ("Tanaka Akira special")
399
400 When we say "call a group," it actually means, "re-execute the subexp in
401 that group."
402
403 \g<0> \g'0' call the whole pattern recursively
404 \g<n> \g'n' (n >= 1) call the nth group
405 \g<-n> \g'-n' (n >= 1) call the nth group counting backwards from
406 the calling position
407 \g<+n> \g'+n' (n >= 1) call the nth group counting forwards from
408 the calling position
409 \g<name> \g'name' call the group with the specified name
410
411 * Left-most recursive calls are not allowed.
412
413 ex. (?<name>a|\g<name>b) => error
414 (?<name>a|b\g<name>c) => OK
415
416 * Calls with a name that is assigned to more than one groups are not
417 allowed in ONIG_SYNTAX_RUBY.
418
419 * Call by number is forbidden if any named group is defined and
420 ONIG_OPTION_CAPTURE_GROUP is not set.
421
422 * The option status of the called group is always effective.
423
424 ex. /(?-i:\g<name>)(?i:(?<name>a)){0}/.match("A")
425
426 * ONIG_SYNTAX_PERL:
427 Use (?&name), (?n), (?-n), (?+n), (?R) or (?0) instead of \g<>.
428 Calls with a name that is assigned to more than one groups are allowed,
429 and the left-most subexp is used.
430
431
432 10. Captured group
433
434 Behavior of an unnamed group (...) changes with the following conditions.
435 (But named group is not changed.)
436
437 case 1. /.../ (named group is not used, no option)
438
439 (...) is treated as a capturing group.
440
441 case 2. /.../g (named group is not used, 'g' option)
442
443 (...) is treated as a non-capturing group (?:...).
444
445 case 3. /..(?<name>..)../ (named group is used, no option)
446
447 (...) is treated as a non-capturing group.
448 numbered-backref/call is not allowed.
449
450 case 4. /..(?<name>..)../G (named group is used, 'G' option)
451
452 (...) is treated as a capturing group.
453 numbered-backref/call is allowed.
454
455 where
456 g: ONIG_OPTION_DONT_CAPTURE_GROUP
457 G: ONIG_OPTION_CAPTURE_GROUP
458
459 ('g' and 'G' options are argued in ruby-dev ML)
460
461
462
463 -----------------------------
464 A-1. Syntax-dependent options
465
466 + ONIG_SYNTAX_RUBY
467 (?m): dot (.) also matches newline
468
469 + ONIG_SYNTAX_PERL, ONIG_SYNTAX_JAVA and ONIG_SYNTAX_PYTHON
470 (?s): dot (.) also matches newline
471 (?m): ^ matches after newline, $ matches before newline
472
473 + ONIG_SYNTAX_PERL
474 (?d), (?l): same as (?u)
475
476
477 A-2. Original extensions
478
479 + hexadecimal digit char type \h, \H
480 + named group (?<name>...), (?'name'...)
481 + named backref \k<name>
482 + subexp call \g<name>, \g<group-num>
483
484
485 A-3. Missing features compared with perl 5.18.0
486
487 + \N{name}, \N{U+xxxx}, \N
488 + \l,\u,\L,\U, \C
489 + \v, \V, \h, \H
490 + (?{code})
491 + (??{code})
492 + (?|...)
493 + (?[])
494 + (*VERB:ARG)
495
496 * \Q...\E
497 This is effective on ONIG_SYNTAX_PERL and ONIG_SYNTAX_JAVA.
498
499
500 A-4. Differences with Japanized GNU regex(version 0.12) of Ruby 1.8
501
502 + add character property (\p{property}, \P{property})
503 + add hexadecimal digit char type (\h, \H)
504 + add look-behind
505 (?<=fixed-width-pattern), (?<!fixed-width-pattern)
506 + add possessive quantifier. ?+, *+, ++
507 + add operations in character class. [], &&
508 ('[' must be escaped as an usual char in character class.)
509 + add named group and subexp call.
510 + octal or hexadecimal number sequence can be treated as
511 a multibyte code char in character class if multibyte encoding
512 is specified.
513 (ex. [\xa1\xa2], [\xa1\xa7-\xa4\xa1])
514 + allow the range of single byte char and multibyte char in character
515 class.
516 ex. /[a-<<any EUC-JP character>>]/ in EUC-JP encoding.
517 + effect range of isolated option is to next ')'.
518 ex. (?:(?i)a|b) is interpreted as (?:(?i:a|b)), not (?:(?i:a)|b).
519 + isolated option is not transparent to previous pattern.
520 ex. a(?i)* is a syntax error pattern.
521 + allowed unpaired left brace as a normal character.
522 ex. /{/, /({)/, /a{2,3/ etc...
523 + negative POSIX bracket [:^xxxx:] is supported.
524 + POSIX bracket [:ascii:] is added.
525 + repeat of look-ahead is not allowed.
526 ex. /(?=a)*/, /(?!b){5}/
527 + Ignore case option is effective to escape sequence.
528 ex. /\x61/i =~ "A"
529 + In the range quantifier, the number of the minimum is optional.
530 /a{,n}/ == /a{0,n}/
531 The omission of both minimum and maximum values is not allowed.
532 /a{,}/
533 + /{n}?/ is not a reluctant quantifier.
534 /a{n}?/ == /(?:a{n})?/
535 + invalid back reference is checked and raises error.
536 /\1/, /(a)\2/
537 + Zero-width match in an infinite loop stops the repeat,
538 then changes of the capture group status are checked as stop condition.
539 /(?:()|())*\1\2/ =~ ""
540 /(?:\1a|())*/ =~ "a"
541
542
543 A-5. Features disabled in default syntax
544
545 + capture history
546
547 (?@...) and (?@<name>...)
548
549 ex. /(?@a)*/.match("aaa") ==> [<0-1>, <1-2>, <2-3>]
550
551 see sample/listcap.c file.
552
553
554 A-6. Problems
555
556 + Invalid encoding byte sequence is not checked.
557
558 ex. UTF-8
559
560 * Invalid first byte is treated as a character.
561 /./u =~ "\xa3"
562
563 * Incomplete byte sequence is not checked.
564 /\w+/ =~ "a\xf3\x8ec"
565
566 // END