Debian Codegraph @

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

raw
1 # Absent Operators
2
3 The absent operators are worth talking about because they are quite uncommon.
4
5 ## Absent Repeater
6
7 An absent repeater node is defined by the syntax `(?~inner_pattern)`, and it will match any text where the inner pattern does not match (i.e. is absent), including across newlines.
8 This does not add any new abilities to the engine, it just allows to clarify intent and to be more easily optimized under the hood. fancy-regex mainly implements this for Oniguruma compatibility.
9
10 It works best or is at least easiest to understand when the inner pattern is a literal.
11
12 ### Example
13
14 Let's imagine you have some Markdown, containing some code fences.
15
16 It might look something like this:
17
18 ````md
19 # Some Heading
20
21 Given a todo list like this:
22
23 **Input:**
24 ```json
25 {
26 "todos": [
27 {
28 "content": "Create `some_helper_func` helper in some_file.rs that takes a closure to check the error",
29 "status": "complete",
30 "priority": "high"
31 },
32 {
33 "content": "Update error-asserting tests in some_file.rs to use `some_helper_func`",
34 "status": "complete",
35 "priority": "high"
36 },
37 {
38 "content": "Run `cargo fmt` and `cargo test`",
39 "status": "pending",
40 "priority": "medium"
41 }
42 ]
43 }
44 ```
45
46 You might expect this output:
47
48 **Output:**
49 ```text
50 High priority tasks have now been completed.
51 ```
52
53 Some more text.
54 ````
55
56 Let's say you want to match all input and output codeblocks.
57
58 Typically you *could* do it like:
59
60 ```regexp
61 [*]{2}(?:In|Out)put:[*]{2}\n```(?:[^`]+|`(?!``))+```
62 ```
63
64 This would match everything inside the codeblock which is not a backtick, or backticks which are not followed by another 2 backticks, until it reaches the 3 backticks marking the end of the codeblock.
65 Generally this type of construct can be quite hard to follow and reason about, to be sure it won't suffer from catastrophic backtracking.
66
67 With the absent repeater, the intention becomes a lot easier to understand - match anything that isn't 3 backticks, followed by 3 backticks.
68 ```regexp
69 [*]{2}(?:In|Out)put:[*]{2}\n```(?~```)```
70 ```
71
72 Where it really shines is when you need more complicated expressions, like to match a variable number of backticks from the code fence boundaries, it becomes a lot easier to read than an expanded alternative which would avoid catastrophic backtracking.
73
74 ```rust
75 use fancy_regex::Regex;
76
77 // Match a code fence: opening backticks (3+), content (absent the same backticks), closing backticks
78 let re = Regex::new(r"(?<!`)(`{3,}(?!`))\w*\n(?~\1)\n(\1)")?;
79
80 // A code fence with 4 backticks, where the inner code contains 3 backticks
81 let input = "````text\nsome code with ``` backticks\n````";
82 let captures = re.captures(input)?.expect("should match");
83
84 // The overall match spans the entire input
85 let m = captures.get(0).unwrap();
86 assert_eq!(m.start(), 0);
87 assert_eq!(m.end(), input.len());
88 assert_eq!(m.as_str(), input);
89
90 // Group 1: the opening 4 backticks
91 let open = captures.get(1).unwrap();
92 assert_eq!(open.as_str(), "````");
93 assert_eq!(open.start(), 0);
94 assert_eq!(open.end(), 4);
95
96 // Group 2: the closing 4 backticks
97 let close = captures.get(2).unwrap();
98 assert_eq!(close.as_str(), "````");
99 assert_eq!(close.start(), input.len() - 4);
100 assert_eq!(close.end(), input.len());
101
102 # Ok::<(), fancy_regex::Error>(())
103 ```
104
105 It also allows the engine to optimize it accordingly.
106
107 ### Other ways of looking at it
108
109 The absent repeater can be considered shorthand for this:
110 ```regexp
111 (?((?!absent))\O|)*
112 ```
113
114 Essentially a conditional, which says when the absent expression doesn't match, match a single character including newlines. When the absent expression does match, match nothing. Repeat greedily.