Debian Codegraph @

debian/rust-vendor/fancy-regex/docs/syntax.md

raw
1 # Syntax
2
3 The regex syntax is based on the [regex] crate's and on Oniguruma, with some additional supported syntax.
4 Where the two conflict, there is a flag to prefer Oniguruma parsing rules. (By default `regex` crate compatible parsing is used.)
5
6 Escapes:
7
8 `\h`
9 : hex digit (`[0-9A-Fa-f]`) \
10 `\H`
11 : not hex digit (`[^0-9A-Fa-f]`) \
12 `\e`
13 : escape control character (`\x1B`) \
14 `\K`
15 : keep text matched so far out of the overall match ([docs](https://www.regular-expressions.info/keep.html))\
16 `\G`
17 : anchor to where the previous match ended ([docs](https://www.regular-expressions.info/continue.html))\
18 `\Z`
19 : anchor to the end of the text before any trailing newlines\
20 `\O`
21 : any character including newline\
22 `\N`
23 : any character except newline\
24 `\R`
25 : general newline - matches all common line break characters: \n, \v, \f, \r, treating \r\n as an atomic unit
26
27 Backreferences:
28
29 `\1`
30 : match the exact string that the first capture group matched \
31 `\2`
32 : backref to the second capture group, etc. \
33 `\k<name>`
34 : match the exact string that the capture group named *name* matched \
35 `(?P=name)`
36 : same as `\k<name>` for compatibility with Python, etc. \
37 `\g<name>`
38 : call the subroutine defined in capture group named *name* \
39 `\g<1>`
40 : call the subroutine defined in capture group 1. Subroutines can be recursive up to 20 levels deep.
41
42 Named capture groups:
43
44 `(?<name>exp)`
45 : match *exp*, creating capture group named *name* \
46 `(?P<name>exp)`
47 : same as `(?<name>exp)` for compatibility with Python, etc.
48
49 Look-around assertions for matching without changing the current position:
50
51 `(?=exp)`
52 : look-ahead, succeeds if *exp* matches to the right of the current position \
53 `(?!exp)`
54 : negative look-ahead, succeeds if *exp* doesn't match to the right \
55 `(?<=exp)`
56 : look-behind, succeeds if *exp* matches to the left of the current position \
57 `(?<!exp)`
58 : negative look-behind, succeeds if *exp* doesn't match to the left
59
60 **Note**: Look-behind assertions with variable length (e.g., `(?<=a+)`) are supported with the
61 `variable-lookbehinds` feature (enabled by default). Without this feature, only constant-length
62 look-behinds are supported. Variable-length look-behinds can include word boundaries and other
63 zero-width assertions (e.g., `(?<=\ba+)`) as long as the rest of the pattern doesn't use
64 backreferences or other "fancy" features that require backtracking within the lookbehind.
65
66 Atomic groups using `(?>exp)` to prevent backtracking within `exp`, e.g.:
67
68 ```rust
69 # use fancy_regex::Regex;
70 let re = Regex::new(r"^a(?>bc|b)c$").unwrap();
71 assert!(re.is_match("abcc").unwrap());
72 // Doesn't match because `|b` is never tried because of the atomic group
73 assert!(!re.is_match("abc").unwrap());
74 ```
75
76 Conditionals - if/then/else:
77
78 `(?(1))`
79 : continue only if first capture group matched \
80 `(?(<name>))` or `(?('name'))`
81 : continue only if capture group named *name* matched \
82 `(?(1)true_branch|false_branch)`
83 : if the first capture group matched then execute the true_branch regex expression, else execute false_branch ([docs](https://www.regular-expressions.info/conditional.html)) \
84 `(?(condition)true_branch|false_branch)`
85 : if the condition matches then execute the true_branch regex expression, else execute false_branch from the point just before the condition was evaluated \
86 `(?(DEFINE)(capture group)(?<named_group>another)`
87 : define capture groups for later use in subroutine calls
88
89 Backtracking control verbs:
90
91 `(*FAIL)`
92 : fail the current backtracking branch
93
94 Absent repeater:
95
96 `(?~abc)`
97 : match anything until `abc` would match or until the end of the haystack if no match