1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*!
With lazy-regex macros, regular expressions
* are checked at compile time, with clear error messages
* are wrapped in `once_cell` lazy static initializers so that they're compiled only once
* can hold flags as suffix: `let case_insensitive_regex = regex!("ab*"i);`
* are defined in a less verbose way
The [`regex!`] macro returns references to normal instances of [`regex::Regex`] or [`regex::bytes::Regex`] so all the usual features are available.
But most often, you won't even use the `regex!` macro but the other macros which are specialized for testing a match, replacing, or capturing groups in some common situations:
* [Test a match](#test-a-match) with [`regex_is_match!`]
* [Extract a value](#extract-a-value) with [`regex_find!`]
* [Capture](#capture) with [`regex_captures!`]
* [Iter on captures](#iter-on-captures) with [`regex_captures_iter!`]
* [Replace with captured groups](#replace-with-captured-groups) with [`regex_replace!`] and [`regex_replace_all!`]
* [Remove part of a string](#remove-part-of-a-string) with [`regex_remove!`]
* [Switch over patterns](#switch-over-patterns) with [`regex_switch!`]
They support the `B` flag for the `regex::bytes::Regex` variant.
All macros exist with a `bytes_` prefix for building `bytes::Regex`, so you also have [`bytes_regex!`], [`bytes_regex_is_match!`], [`bytes_regex_find!`], [`bytes_regex_captures!`], [`bytes_regex_replace!`], [`bytes_regex_replace_all!`], and [`bytes_regex_switch!`].
Some structs of the regex crate are reexported to ease dependency managment.
# Build Regexes
Build a simple regex:
```rust
# use lazy_regex::regex;
let r = regex!("sa+$");
assert_eq!(r.is_match("Saa"), false);
```
Build a regex with flag(s):
```rust
# use lazy_regex::regex;
let r = regex!("sa+$"i);
assert_eq!(r.is_match("Saa"), true);
```
You can use a raw literal:
```rust
# use lazy_regex::regex;
let r = regex!(r#"^"+$"#);
assert_eq!(r.is_match("\"\""), true);
```
Or a raw literal with flag(s):
```rust
# use lazy_regex::regex;
let r = regex!(r#"^\s*("[a-t]*"\s*)+$"#i);
assert_eq!(r.is_match(r#" "Aristote" "Platon" "#), true);
```
Build a regex that operates on `&[u8]`:
```rust
# use lazy_regex::regex;
let r = regex!("(byte)?string$"B);
assert_eq!(r.is_match(b"bytestring"), true);
```
There's no problem using the multiline definition syntax:
```rust
# use lazy_regex::regex;
let r = regex!(r"(?x)
(?P<name>\w+)
-
(?P<version>[0-9.]+)
");
assert_eq!(r.find("This is lazy_regex-2.2!").unwrap().as_str(), "lazy_regex-2.2");
```
(look at the `regex_captures!` macro to easily extract the groups)
This line doesn't compile because the regex is invalid:
```compile_fail
let r = regex!("(unclosed");
```
Supported regex flags: [`i`, `m`, `s`, `x`, `U`][regex::RegexBuilder], and you may also use `B` to build a bytes regex.
The following regexes are equivalent:
* `bytes_regex!("^ab+$"i)`
* `bytes_regex!("(?i)^ab+$")`
* `regex!("^ab+$"iB)`
* `regex!("(?i)^ab+$"B)`
They're all case insensitive instances of `regex::bytes::Regex`.
# Test a match
```rust
use lazy_regex::*;
let b = regex_is_match!("[ab]+", "car");
assert_eq!(b, true);
let b = bytes_regex_is_match!("[ab]+", b"car");
assert_eq!(b, true);
```
See [`regex_is_match!`]
# Extract a value
```rust
use lazy_regex::regex_find;
let f_word = regex_find!(r"\bf\w+\b", "The fox jumps.");
assert_eq!(f_word, Some("fox"));
let f_word = regex_find!(r"\bf\w+\b"B, b"The forest is silent.");
assert_eq!(f_word, Some(b"forest" as &[u8]));
```
See [`regex_find!`]
# Capture
```rust
use lazy_regex::regex_captures;
let (_, letter) = regex_captures!("([a-z])[0-9]+"i, "form A42").unwrap();
assert_eq!(letter, "A");
let (whole, name, version) = regex_captures!(
r"(\w+)-([0-9.]+)", // a literal regex
"This is lazy_regex-2.0!", // any expression
).unwrap();
assert_eq!(whole, "lazy_regex-2.0");
assert_eq!(name, "lazy_regex");
assert_eq!(version, "2.0");
```
There's no limit to the size of the tuple.
It's checked at compile time to ensure you have the right number of capturing groups.
You receive `""` for optional groups with no value.
See [`regex_captures!`]
# Iter on captures
```rust
use lazy_regex::regex_captures_iter;
let hay = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
let mut movies = vec![];
let iter = regex_captures_iter!(r"'([^']+)'\s+\(([0-9]{4})\)", hay);
for (_, [title, year]) in iter.map(|c| c.extract()) {
movies.push((title, year.parse::<i64>().unwrap()));
}
assert_eq!(movies, vec![
("Citizen Kane", 1941),
("The Wizard of Oz", 1939),
("M", 1931),
]);
```
See [`regex_captures_iter!`]
# Replace with captured groups
The [`regex_replace!`] and [`regex_replace_all!`] macros bring once compilation and compilation time checks to the `replace` and `replace_all` functions.
## Replace with a closure
```rust
use lazy_regex::regex_replace_all;
let text = "Foo8 fuu3";
let text = regex_replace_all!(
r"\bf(\w+)(\d)"i,
text,
|_, name, digit| format!("F<{}>{}", name, digit),
);
assert_eq!(text, "F<oo>8 F<uu>3");
```
The number of arguments given to the closure is checked at compilation time to match the number of groups in the regular expression.
If it doesn't match you get a clear error message at compilation time.
## Replace with another kind of Replacer
```rust
use lazy_regex::regex_replace_all;
let text = "UwU";
let output = regex_replace_all!("U", text, "O");
assert_eq!(&output, "OwO");
```
# Remove part of a string
`regex_remove!` is cleaner than using `regex_replace!` with an empty string.
Contrary to replace, it doesn't allocate a new string if the match is at an end of the input, which makes it especially useful for trimming suffixes or prefixes.
```rust
use lazy_regex::regex_remove;
let text = "lazy-regex-3.5.0";
let name = regex_remove!(
r"-[0-9]+(\.[0-9]+)*$",
text,
);
assert_eq!(name, "lazy-regex");
assert!(matches!(name, std::borrow::Cow::Borrowed(_)));
```
# Switch over patterns
Execute the expression bound to the first matching regex, with named captured groups declared as variables:
```rust
use lazy_regex::regex_switch;
#[derive(Debug, PartialEq)]
pub enum ScrollCommand {
Top,
Bottom,
Lines(i32),
Pages(i32),
JumpTo(String),
}
impl std::str::FromStr for ScrollCommand {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
regex_switch!(s,
"^scroll-to-top$" => Self::Top,
"^scroll-to-bottom$" => Self::Bottom,
r"^scroll-lines?\((?<n>[+-]?\d{1,4})\)$" => Self::Lines(n.parse().unwrap()),
r"^scroll-pages?\((?<n>[+-]?\d{1,4})\)$" => Self::Pages(n.parse().unwrap()),
r"^jump-to\((?<name>\w+)\)$" => Self::JumpTo(name.to_string()),
).ok_or("unknown command")
}
}
assert_eq!("scroll-lines(42)".parse(), Ok(ScrollCommand::Lines(42)));
assert_eq!("scroll-lines(XLII)".parse::<ScrollCommand>(), Err("unknown command"));
```
See [`regex_switch!`]
# Shared lazy static
When a regular expression is used in several functions, you sometimes don't want
to repeat it but have a shared static instance.
The [`regex!`] macro, while being backed by a lazy static regex, returns a reference.
If you want to have a shared lazy static regex, use the [`lazy_regex!`] macro:
```rust
# use lazy_regex::*;
pub static GLOBAL_REX: Lazy<Regex> = lazy_regex!("^ab+$"i);
```
Like for the other macros, the regex is static, checked at compile time, and lazily built at first use.
See [`lazy_regex!`]
*/
pub use ;
pub use ;
pub use ;