Debian Codegraph @

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

raw
1 An implementation of regexes, supporting a relatively rich set of features, including backreferences
2 and lookaround. Aims to be compatible with Oniguruma syntax when the relevant flag is set.
3
4 It builds on top of the excellent [regex] crate. If you are not
5 familiar with it, make sure you read its documentation and maybe you don't even need fancy-regex.
6
7 If your regex or parts of it does not use any special features, the matching is delegated to the
8 regex crate. That means it has linear runtime. But if you use "fancy" features such as
9 backreferences or look-around, an engine with backtracking needs to be used. In that case, the regex
10 can be slow and take exponential time to run because of what is called "catastrophic backtracking".
11 This depends on the regex and the input.
12
13 # Usage
14
15 The API should feel very similar to the regex crate, and involves compiling a regex and then using
16 it to find matches in text.
17
18 ## Example: Matching text
19
20 An example with backreferences to check if a text consists of two identical words:
21
22 ```rust
23 use fancy_regex::Regex;
24
25 let re = Regex::new(r"^(\w+) (\1)$").unwrap();
26 let result = re.is_match("foo foo");
27
28 assert!(result.is_ok());
29 let did_match = result.unwrap();
30 assert!(did_match);
31 ```
32
33 Note that like in the regex crate, the regex needs anchors like `^` and `$` to match against the
34 entire input text.
35
36 ## Example: Finding the position of matches
37
38 ```rust
39 use fancy_regex::Regex;
40
41 let re = Regex::new(r"(\d)\1").unwrap();
42 let result = re.find("foo 22");
43
44 assert!(result.is_ok(), "execution was successful");
45 let match_option = result.unwrap();
46
47 assert!(match_option.is_some(), "found a match");
48 let m = match_option.unwrap();
49
50 assert_eq!(m.start(), 4);
51 assert_eq!(m.end(), 6);
52 assert_eq!(m.as_str(), "22");
53 ```
54
55 ## Example: Capturing groups
56
57 ```rust
58 use fancy_regex::Regex;
59
60 let re = Regex::new(r"(?<!AU)\$(\d+)").unwrap();
61 let result = re.captures("AU$10, $20");
62
63 let captures = result.expect("Error running regex").expect("No match found");
64 let group = captures.get(1).expect("No group");
65 assert_eq!(group.as_str(), "20");
66 ```
67
68 ## Example: Searching within a range without slicing
69
70 ```rust
71 use fancy_regex::{Regex, RegexInput};
72
73 let re = Regex::new(r"\bat\b").unwrap();
74 let haystack = "batter at";
75
76 assert!(re.find("at").unwrap().is_some());
77 assert!(re
78 .find_input(RegexInput::new(haystack).range(1..3))
79 .unwrap()
80 .is_none());
81 ```
82
83 Using [`RegexInput`] preserves the original haystack for anchors, word
84 boundaries, and lookaround while still constraining the reported match to a
85 specific byte range.
86
87 ## RegexSet notes for `regex` crate users
88
89 `fancy_regex::RegexSet` is not a drop-in API replacement for `regex::RegexSet`.
90
91 - `regex::RegexSet` answers "which patterns matched anywhere?"
92 - `fancy_regex::RegexSet` answers "what matched at the earliest position?" and yields
93 full matches (with offsets and captures) in pattern index order.
94 - Each yielded match is a `Result`, since fancy features may fail at runtime (for
95 example when a backtracking limit is exceeded).
96
97 This design is useful for tasks like tokenization/syntax-highlighting where you
98 need both match locations and explicit control over priority resolution.
99
100 ## Example: Splitting text
101
102 ```rust
103 use fancy_regex::Regex;
104
105 let re = Regex::new(r"[ \t]+").unwrap();
106 let target = "a b \t c\td e";
107 let fields: Vec<&str> = re.split(target).map(|x| x.unwrap()).collect();
108 assert_eq!(fields, vec!["a", "b", "c", "d", "e"]);
109
110 let fields: Vec<&str> = re.splitn(target, 3).map(|x| x.unwrap()).collect();
111 assert_eq!(fields, vec!["a", "b", "c\td e"]);
112 ```
113
114 [regex]: https://crates.io/crates/regex