| 1 |
use fancy_regex::{ |
| 2 |
BytesMode, Captures, CompileError, Error, Expander, Match, RegexBuilder, RegexInput, Result, |
| 3 |
}; |
| 4 |
use matches::assert_matches; |
| 5 |
use std::borrow::Cow; |
| 6 |
use std::ops::Index; |
| 7 |
use std::ops::Range; |
| 8 |
|
| 9 |
mod common; |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn capture_names() { |
| 13 |
let regex = common::regex("(?<foo>)()(?P<bar>)"); |
| 14 |
let capture_names = regex.capture_names().collect::<Vec<_>>(); |
| 15 |
assert_eq!(capture_names, vec![None, Some("foo"), None, Some("bar")]); |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn captures_fancy() { |
| 20 |
let captures = common::assert_captures(r"\s*(\w+)(?=\.)", "foo bar.").unwrap(); |
| 21 |
assert_eq!(captures.len(), 2); |
| 22 |
assert_match(captures.get(0), " bar", 3, 7); |
| 23 |
assert_match(captures.get(1), "bar", 4, 7); |
| 24 |
assert!(captures.get(2).is_none()); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn nested_repeats_preserve_capture_numbering() { |
| 29 |
let re = RegexBuilder::new(r"(x+){1,}(y)$") |
| 30 |
.oniguruma_mode(true) |
| 31 |
.build() |
| 32 |
.unwrap(); |
| 33 |
let captures = re.captures("xxxy").unwrap().unwrap(); |
| 34 |
assert_eq!(captures.len(), 3); |
| 35 |
assert_match(captures.get(1), "xxx", 0, 3); |
| 36 |
assert_match(captures.get(2), "y", 3, 4); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn captures_fancy_named() { |
| 41 |
let captures = common::assert_captures(r"\s*(?<name>\w+)(?=\.)", "foo bar.").unwrap(); |
| 42 |
assert_eq!(captures.len(), 2); |
| 43 |
assert_match(captures.get(0), " bar", 3, 7); |
| 44 |
assert_match(captures.name("name"), "bar", 4, 7); |
| 45 |
assert_eq!(captures.index(0), " bar"); |
| 46 |
assert_eq!(captures.index("name"), "bar"); |
| 47 |
assert!(captures.get(2).is_none()); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn captures_fancy_unmatched_group() { |
| 52 |
let captures = common::assert_captures(r"(\w+)(?=\.)|(\w+)(?=!)", "foo! bar.").unwrap(); |
| 53 |
assert_eq!(captures.len(), 3); |
| 54 |
assert_match(captures.get(0), "foo", 0, 3); |
| 55 |
assert!(captures.get(1).is_none()); |
| 56 |
assert_match(captures.get(2), "foo", 0, 3); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn optional_capture_group_unmatched_is_none() { |
| 61 |
let captures = common::assert_captures(r"(\w+)?@(\w+).", "@host.").unwrap(); |
| 62 |
assert!(captures.get(1).is_none()); |
| 63 |
assert_match(captures.get(2), "host", 1, 5); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn optional_capture_group_matched_is_some() { |
| 68 |
let captures = common::assert_captures(r"(\w+)?@(\w+).", "user@host.").unwrap(); |
| 69 |
assert_match(captures.get(1), "user", 0, 4); |
| 70 |
assert_match(captures.get(2), "host", 5, 9); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn captures_after_lookbehind() { |
| 75 |
let captures = captures( |
| 76 |
r"\s*(?<=[() ])(@\w+)(\([^)]*\))?\s*", |
| 77 |
" @another(foo bar) ", |
| 78 |
); |
| 79 |
assert_match(captures.get(1), "@another", 1, 9); |
| 80 |
assert_match(captures.get(2), "(foo bar)", 9, 18); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
#[cfg(feature = "variable-lookbehinds")] |
| 85 |
fn non_empty_captures_inside_variable_lookbehind() { |
| 86 |
let captures = captures(r"(?<=(a+)(b+))x", "aaabbbx"); |
| 87 |
assert_eq!(captures.len(), 3); |
| 88 |
assert_match(captures.get(0), "x", 6, 7); |
| 89 |
assert_match(captures.get(1), "aaa", 0, 3); |
| 90 |
assert_match(captures.get(2), "bbb", 3, 6); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
#[cfg(feature = "variable-lookbehinds")] |
| 95 |
fn empty_capture_inside_variable_lookbehind() { |
| 96 |
let captures = captures(r"(?<=(a+)(b*))c", "aaac"); |
| 97 |
assert_eq!(captures.len(), 3); |
| 98 |
assert_match(captures.get(0), "c", 3, 4); |
| 99 |
assert_match(captures.get(1), "aaa", 0, 3); |
| 100 |
// Empty match for b* since there are no b's |
| 101 |
assert_match(captures.get(2), "", 3, 3); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
#[cfg(feature = "variable-lookbehinds")] |
| 106 |
fn named_captures_inside_variable_lookbehind() { |
| 107 |
let captures = captures(r"(?<=(?<first>a+)(?<second>b+))x", "aaabbbx"); |
| 108 |
assert_eq!(captures.len(), 3); |
| 109 |
assert_match(captures.get(0), "x", 6, 7); |
| 110 |
assert_match(captures.name("first"), "aaa", 0, 3); |
| 111 |
assert_match(captures.name("second"), "bbb", 3, 6); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
#[cfg(feature = "variable-lookbehinds")] |
| 116 |
fn captures_both_inside_and_outside_variable_lookbehind() { |
| 117 |
let captures = captures(r"(?<=(a+)(b+))(c+)(d+)", "aaabbbcccddd"); |
| 118 |
assert_eq!(captures.len(), 5); |
| 119 |
assert_match(captures.get(0), "cccddd", 6, 12); |
| 120 |
assert_match(captures.get(1), "aaa", 0, 3); |
| 121 |
assert_match(captures.get(2), "bbb", 3, 6); |
| 122 |
assert_match(captures.get(3), "ccc", 6, 9); |
| 123 |
assert_match(captures.get(4), "ddd", 9, 12); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn captures_with_keepout_inside_at_end() { |
| 128 |
let captures = captures(r"\s*(\w+\K)(?=\.)", "foo bar."); |
| 129 |
assert_eq!(captures.len(), 2); |
| 130 |
assert_match(captures.get(0), "", 7, 7); |
| 131 |
assert_match(captures.get(1), "bar", 4, 7); |
| 132 |
assert!(captures.get(2).is_none()); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn captures_with_keepout_inside_in_middle() { |
| 137 |
let captures = captures(r"\s*(b\Kar)(?=\.)", "foo bar."); |
| 138 |
assert_eq!(captures.len(), 2); |
| 139 |
assert_match(captures.get(0), "ar", 5, 7); |
| 140 |
assert_match(captures.get(1), "bar", 4, 7); |
| 141 |
assert!(captures.get(2).is_none()); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn captures_with_keepout_between() { |
| 146 |
let captures = captures(r"(\w+)\K\s*(\w+)(?=\.)", "foo bar."); |
| 147 |
assert_eq!(captures.len(), 3); |
| 148 |
assert_match(captures.get(0), " bar", 3, 7); |
| 149 |
assert_match(captures.get(1), "foo", 0, 3); |
| 150 |
assert_match(captures.get(2), "bar", 4, 7); |
| 151 |
assert!(captures.get(3).is_none()); |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn captures_with_nested_keepout() { |
| 156 |
let captures = captures(r"(\w\K)+\s*(\w+)(?=\.)", "foo bar."); |
| 157 |
assert_eq!(captures.len(), 3); |
| 158 |
assert_match(captures.get(0), " bar", 3, 7); |
| 159 |
assert_match(captures.get(1), "o", 2, 3); |
| 160 |
assert_match(captures.get(2), "bar", 4, 7); |
| 161 |
assert!(captures.get(3).is_none()); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn captures_with_conditional() { |
| 166 |
let captures = captures(r"^(?((ab))c|d)$", "abc"); |
| 167 |
assert_eq!(captures.len(), 2); |
| 168 |
assert_match(captures.get(0), "abc", 0, 3); |
| 169 |
assert_match(captures.get(1), "ab", 0, 2); |
| 170 |
assert!(captures.get(2).is_none()); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn capture_with_crlf_flag() { |
| 175 |
// The following test cases are adopted from the test cases of the `regex-automata` crate: |
| 176 |
// https://github.com/rust-lang/regex/blob/c79c40a39ebc4ffdc9b886e34806b9085583b769/testdata/crlf.toml |
| 177 |
assert_capture_with_crlf_flag( |
| 178 |
r"(?mR)^[a-z]+$", |
| 179 |
"abc\r\ndef\r\nxyz", |
| 180 |
vec!["abc", "def", "xyz"], |
| 181 |
vec![(0..3), (5..8), (10..13)], |
| 182 |
); |
| 183 |
assert_capture_with_crlf_flag(r"(?mR)^$", "abc\r\ndef\r\nxyz", vec![], vec![]); |
| 184 |
assert_capture_with_crlf_flag(r"(?mR)^$", "", vec![""], vec![(0..0)]); |
| 185 |
assert_capture_with_crlf_flag(r"(?mR)^$", "\r\n", vec!["", ""], vec![(0..0), (2..2)]); |
| 186 |
assert_capture_with_crlf_flag( |
| 187 |
r"(?mR)^", |
| 188 |
"abc\r\ndef\r\nxyz", |
| 189 |
vec!["", "", ""], |
| 190 |
vec![(0..0), (5..5), (10..10)], |
| 191 |
); |
| 192 |
assert_capture_with_crlf_flag( |
| 193 |
r"(?mR)^", |
| 194 |
"\r\n\r\n\r\n", |
| 195 |
vec!["", "", "", ""], |
| 196 |
vec![(0..0), (2..2), (4..4), (6..6)], |
| 197 |
); |
| 198 |
assert_capture_with_crlf_flag( |
| 199 |
r"(?mR)^", |
| 200 |
"\r\r\r", |
| 201 |
vec!["", "", "", ""], |
| 202 |
vec![(0..0), (1..1), (2..2), (3..3)], |
| 203 |
); |
| 204 |
assert_capture_with_crlf_flag( |
| 205 |
r"(?mR)^", |
| 206 |
"\n\n\n", |
| 207 |
vec!["", "", "", ""], |
| 208 |
vec![(0..0), (1..1), (2..2), (3..3)], |
| 209 |
); |
| 210 |
assert_capture_with_crlf_flag( |
| 211 |
r"(?mR)$", |
| 212 |
"abc\r\ndef\r\nxyz", |
| 213 |
vec!["", "", ""], |
| 214 |
vec![(3..3), (8..8), (13..13)], |
| 215 |
); |
| 216 |
assert_capture_with_crlf_flag( |
| 217 |
r"(?mR)$", |
| 218 |
"\r\n\r\n\r\n", |
| 219 |
vec!["", "", "", ""], |
| 220 |
vec![(0..0), (2..2), (4..4), (6..6)], |
| 221 |
); |
| 222 |
assert_capture_with_crlf_flag( |
| 223 |
r"(?mR)$", |
| 224 |
"\r\r\r", |
| 225 |
vec!["", "", "", ""], |
| 226 |
vec![(0..0), (1..1), (2..2), (3..3)], |
| 227 |
); |
| 228 |
assert_capture_with_crlf_flag( |
| 229 |
r"(?mR)$", |
| 230 |
"\n\n\n", |
| 231 |
vec!["", "", "", ""], |
| 232 |
vec![(0..0), (1..1), (2..2), (3..3)], |
| 233 |
); |
| 234 |
// In CRLF mode, `.` should not match `\r` or `\n` |
| 235 |
assert_capture_with_crlf_flag(r"(?R).", "\r\n\r\n\r\n", vec![], vec![]); |
| 236 |
} |
| 237 |
|
| 238 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 239 |
fn assert_capture_with_crlf_flag( |
| 240 |
regex_src: &str, |
| 241 |
text: &str, |
| 242 |
expected_texts: Vec<&str>, |
| 243 |
expected_spans: Vec<Range<usize>>, |
| 244 |
) { |
| 245 |
fn assert_capture_matches( |
| 246 |
regex_src: &str, |
| 247 |
text: &str, |
| 248 |
expected_texts: &[&str], |
| 249 |
expected_spans: &[Range<usize>], |
| 250 |
) { |
| 251 |
let regex = common::regex(regex_src); |
| 252 |
let capture_matches: Vec<_> = regex.captures_iter(text).map(|c| c.unwrap()).collect(); |
| 253 |
assert_eq!( |
| 254 |
capture_matches.len(), |
| 255 |
expected_texts.len(), |
| 256 |
"Pattern '{}' on '{}': expected {} matches, got {}", |
| 257 |
regex_src, |
| 258 |
text.escape_default(), |
| 259 |
expected_texts.len(), |
| 260 |
capture_matches.len(), |
| 261 |
); |
| 262 |
for (captures, (&expected_text, expected_span)) in capture_matches |
| 263 |
.iter() |
| 264 |
.zip(expected_texts.iter().zip(expected_spans.iter())) |
| 265 |
{ |
| 266 |
assert_match( |
| 267 |
captures.get(0), |
| 268 |
expected_text, |
| 269 |
expected_span.start, |
| 270 |
expected_span.end, |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
fn make_harder(regex_src: &str) -> String { |
| 276 |
// Wrap the original regex source in an atomic group to make it `hard` so that the execution of the regex is |
| 277 |
// handled by the backtracking VM engine implemented in `fancy-regex` rather than delegated to the engine of |
| 278 |
// `regex-automata`. |
| 279 |
format!(r"(?>{})", regex_src) |
| 280 |
} |
| 281 |
|
| 282 |
// Verify that the regex-automata's engine is able to handle the CRLF flag. |
| 283 |
assert_capture_matches(regex_src, text, &expected_texts, &expected_spans); |
| 284 |
|
| 285 |
// Verify that the fancy-regex's backtracking VM engine is able to handle the CRLF flag. |
| 286 |
assert_capture_matches( |
| 287 |
make_harder(regex_src).as_str(), |
| 288 |
text, |
| 289 |
&expected_texts, |
| 290 |
&expected_spans, |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn captures_iter() { |
| 296 |
let text = "11 21 33"; |
| 297 |
|
| 298 |
let all_captures = common::assert_captures_iter(r"(?P<num>\d)\d", text); |
| 299 |
assert_eq!(all_captures.len(), 3); |
| 300 |
|
| 301 |
assert_eq!(all_captures[0].len(), 2); |
| 302 |
assert_match(all_captures[0].get(0), "11", 0, 2); |
| 303 |
assert_match(all_captures[0].name("num"), "1", 0, 1); |
| 304 |
|
| 305 |
assert_eq!(all_captures[1].len(), 2); |
| 306 |
assert_match(all_captures[1].get(0), "21", 3, 5); |
| 307 |
assert_match(all_captures[1].name("num"), "2", 3, 4); |
| 308 |
|
| 309 |
assert_eq!(all_captures[2].len(), 2); |
| 310 |
assert_match(all_captures[2].get(0), "33", 6, 8); |
| 311 |
assert_match(all_captures[2].name("num"), "3", 6, 7); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn captures_iter_attributes() { |
| 316 |
let text = "11 21 33"; |
| 317 |
let regex = common::regex(r"(?P<num>\d)\d"); |
| 318 |
|
| 319 |
let all_captures = regex.captures_iter(text); |
| 320 |
|
| 321 |
assert_eq!(all_captures.text(), text); |
| 322 |
assert_eq!(regex.as_str(), all_captures.regex().as_str()); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn captures_iter_continue_from_previous_match_end() { |
| 327 |
let text = "1122 33"; |
| 328 |
|
| 329 |
let all_caps = common::assert_captures_iter(r"\G(\d)\d", text); |
| 330 |
assert_eq!(all_caps.len(), 2); |
| 331 |
assert_eq!(all_caps[0].get(0).unwrap().start(), 0); |
| 332 |
assert_eq!(all_caps[0].get(0).unwrap().end(), 2); |
| 333 |
assert_eq!(all_caps[0].get(1).unwrap().start(), 0); |
| 334 |
assert_eq!(all_caps[0].get(1).unwrap().end(), 1); |
| 335 |
assert_eq!(all_caps[1].get(0).unwrap().start(), 2); |
| 336 |
assert_eq!(all_caps[1].get(0).unwrap().end(), 4); |
| 337 |
assert_eq!(all_caps[1].get(1).unwrap().start(), 2); |
| 338 |
assert_eq!(all_caps[1].get(1).unwrap().end(), 3); |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn captures_iter_continue_from_previous_match_end_with_zero_width_match() { |
| 343 |
let text = "1122 33"; |
| 344 |
|
| 345 |
for (i, caps) in common::regex(r"\G\d*").captures_iter(text).enumerate() { |
| 346 |
let caps = caps.unwrap(); |
| 347 |
|
| 348 |
match i { |
| 349 |
0 => { |
| 350 |
assert_eq!(caps.get(0).unwrap().start(), 0); |
| 351 |
assert_eq!(caps.get(0).unwrap().end(), 4); |
| 352 |
} |
| 353 |
i => panic!("Expected 1 result, got {}", i + 1), |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
#[test] |
| 359 |
fn captures_iter_continue_from_previous_match_end_single_match() { |
| 360 |
let text = "123 456 789"; |
| 361 |
|
| 362 |
for (i, caps) in common::regex(r"\G(\d+)").captures_iter(text).enumerate() { |
| 363 |
let caps = caps.unwrap(); |
| 364 |
|
| 365 |
match i { |
| 366 |
0 => { |
| 367 |
assert_eq!(caps.get(0).unwrap().start(), 0); |
| 368 |
assert_eq!(caps.get(0).unwrap().end(), 3); |
| 369 |
assert_eq!(caps.get(1).unwrap().start(), 0); |
| 370 |
assert_eq!(caps.get(1).unwrap().end(), 3); |
| 371 |
} |
| 372 |
i => panic!("Expected 1 result, got {}", i + 1), |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
#[test] |
| 378 |
fn captures_iter_continue_from_previous_match_end_with_keepout() { |
| 379 |
// \G..\K(?=(.)) — \K resets the match start to after two consumed bytes, producing |
| 380 |
// zero-length group 0 matches. Iteration must not stop after the first match because |
| 381 |
// the VM actually consumed real bytes before \K fired, so \G should keep succeeding. |
| 382 |
let text = "aabbcc"; |
| 383 |
|
| 384 |
// Each match: group 0 is zero-length at the position after the two consumed chars, |
| 385 |
// group 1 captures the next single char via the lookahead. |
| 386 |
for (i, caps) in common::regex(r"\G..\K(?=(.))") |
| 387 |
.captures_iter(text) |
| 388 |
.enumerate() |
| 389 |
{ |
| 390 |
let caps = caps.unwrap(); |
| 391 |
|
| 392 |
match i { |
| 393 |
0 => { |
| 394 |
assert_eq!(caps.get(0).unwrap().start(), 2); |
| 395 |
assert_eq!(caps.get(0).unwrap().end(), 2); |
| 396 |
assert_eq!(caps.get(1).unwrap().start(), 2); |
| 397 |
assert_eq!(caps.get(1).unwrap().end(), 3); |
| 398 |
} |
| 399 |
1 => { |
| 400 |
assert_eq!(caps.get(0).unwrap().start(), 5); |
| 401 |
assert_eq!(caps.get(0).unwrap().end(), 5); |
| 402 |
assert_eq!(caps.get(1).unwrap().start(), 5); |
| 403 |
assert_eq!(caps.get(1).unwrap().end(), 6); |
| 404 |
} |
| 405 |
i => panic!("Expected 2 results, got {}", i + 1), |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn captures_from_pos() { |
| 412 |
let text = "11 21 33"; |
| 413 |
|
| 414 |
let captures = common::assert_captures_from_pos(r"(\d)\d", text, 3).unwrap(); |
| 415 |
assert_eq!(captures.len(), 2); |
| 416 |
assert_match(captures.get(0), "21", 3, 5); |
| 417 |
assert_match(captures.get(1), "2", 3, 4); |
| 418 |
let matches: Vec<_> = captures.iter().collect(); |
| 419 |
assert_eq!(matches.len(), 2); |
| 420 |
assert_match(matches[0], "21", 3, 5); |
| 421 |
assert_match(matches[1], "2", 3, 4); |
| 422 |
|
| 423 |
let captures = common::assert_captures_from_pos(r"(\d+)\1", text, 3).unwrap(); |
| 424 |
assert_eq!(captures.len(), 2); |
| 425 |
assert_match(captures.get(0), "33", 6, 8); |
| 426 |
assert_match(captures.get(1), "3", 6, 7); |
| 427 |
let matches: Vec<_> = captures.iter().collect(); |
| 428 |
assert_eq!(matches.len(), 2); |
| 429 |
assert_match(matches[0], "33", 6, 8); |
| 430 |
assert_match(matches[1], "3", 6, 7); |
| 431 |
|
| 432 |
let captures = common::assert_captures_from_pos(r"(?P<foo>\d+)\k<foo>", text, 3).unwrap(); |
| 433 |
assert_eq!(captures.len(), 2); |
| 434 |
assert_match(captures.get(0), "33", 6, 8); |
| 435 |
assert_match(captures.name("foo"), "3", 6, 7); |
| 436 |
let matches: Vec<_> = captures.iter().collect(); |
| 437 |
assert_eq!(matches.len(), 2); |
| 438 |
assert_match(matches[0], "33", 6, 8); |
| 439 |
assert_match(matches[1], "3", 6, 7); |
| 440 |
|
| 441 |
let captures = common::assert_captures_from_pos(r"(?P<foo>\d+)(?P=foo)", text, 3).unwrap(); |
| 442 |
assert_eq!(captures.len(), 2); |
| 443 |
assert_match(captures.get(0), "33", 6, 8); |
| 444 |
assert_match(captures.name("foo"), "3", 6, 7); |
| 445 |
let matches: Vec<_> = captures.iter().collect(); |
| 446 |
assert_eq!(matches.len(), 2); |
| 447 |
assert_match(matches[0], "33", 6, 8); |
| 448 |
assert_match(matches[1], "3", 6, 7); |
| 449 |
} |
| 450 |
|
| 451 |
#[test] |
| 452 |
fn captures_from_pos_past_end_wrap() { |
| 453 |
let re = fancy_regex::RegexBuilder::new(r"^(<{7})(?:\s+(\S.*?))?$\n?") |
| 454 |
.oniguruma_mode(true) |
| 455 |
.build() |
| 456 |
.unwrap(); |
| 457 |
let result = re.captures_from_pos("ab", 12); |
| 458 |
assert_matches!(result, Ok(None)); |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn captures_from_pos_past_end_fancy() { |
| 463 |
let re = fancy_regex::RegexBuilder::new(r"(?<=a)b") |
| 464 |
.oniguruma_mode(true) |
| 465 |
.build() |
| 466 |
.unwrap(); |
| 467 |
let result = re.captures_from_pos("ab", 12); |
| 468 |
assert_matches!(result, Ok(None)); |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn captures_from_pos_at_end_still_runs() { |
| 473 |
let re = fancy_regex::RegexBuilder::new("$") |
| 474 |
.oniguruma_mode(true) |
| 475 |
.build() |
| 476 |
.unwrap(); |
| 477 |
let result = re.captures_from_pos("ab", 2).unwrap(); |
| 478 |
assert!(result.is_some(), "pattern `$` must match at end-of-text"); |
| 479 |
} |
| 480 |
|
| 481 |
#[test] |
| 482 |
fn captures_from_pos_looking_left() { |
| 483 |
let regex = common::regex(r"\b(\w)"); |
| 484 |
|
| 485 |
// This should *not* match because `\b` doesn't match between a and x |
| 486 |
let result = regex.captures_from_pos("ax", 1).unwrap(); |
| 487 |
assert!(result.is_none()); |
| 488 |
|
| 489 |
let captures = assert_captures(regex.captures_from_pos(".x", 1)); |
| 490 |
assert_eq!(captures.len(), 2); |
| 491 |
assert_match(captures.get(0), "x", 1, 2); |
| 492 |
assert_match(captures.get(1), "x", 1, 2); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn captures_input_wrap_range_keeps_anchor_context() { |
| 497 |
assert!(common::assert_captures_input(r"\bat\b", "batter", 0, 1..3).is_none()); |
| 498 |
|
| 499 |
let captures = common::assert_captures_input(r"(?m)^foo$", "xx\nfoo\nzz", 0, 3..6).unwrap(); |
| 500 |
assert_match(captures.get(0), "foo", 3, 6); |
| 501 |
} |
| 502 |
|
| 503 |
#[test] |
| 504 |
fn captures_input_fancy_range_allows_lookaround_outside_range() { |
| 505 |
let captures = common::assert_captures_input(r"(?=(foo))\1(?=bar)", "foobar", 0, 0..3).unwrap(); |
| 506 |
assert_match(captures.get(0), "foo", 0, 3); |
| 507 |
assert_match(captures.get(1), "foo", 0, 3); |
| 508 |
|
| 509 |
let captures = common::assert_captures_input(r"(?<=foo)(bar)", "foobar", 3, 3..6).unwrap(); |
| 510 |
assert_match(captures.get(0), "bar", 3, 6); |
| 511 |
assert_match(captures.get(1), "bar", 3, 6); |
| 512 |
} |
| 513 |
|
| 514 |
#[test] |
| 515 |
fn captures_input_text_boundary_overrides() { |
| 516 |
let re = RegexBuilder::new(r"\A(foo)\z") |
| 517 |
.allow_input_assertion_overrides(true) |
| 518 |
.build() |
| 519 |
.unwrap(); |
| 520 |
let re_bytes = RegexBuilder::new(r"\A(foo)\z") |
| 521 |
.bytes_mode(BytesMode::Ascii) |
| 522 |
.allow_input_assertion_overrides(true) |
| 523 |
.build() |
| 524 |
.unwrap(); |
| 525 |
let caps = re |
| 526 |
.captures_input(RegexInput::new("foo").start_text(false).end_text(false)) |
| 527 |
.unwrap(); |
| 528 |
assert!(caps.is_none()); |
| 529 |
let caps = re_bytes |
| 530 |
.captures_input( |
| 531 |
RegexInput::new("foo".as_bytes()) |
| 532 |
.start_text(false) |
| 533 |
.end_text(false), |
| 534 |
) |
| 535 |
.unwrap(); |
| 536 |
assert!(caps.is_none()); |
| 537 |
|
| 538 |
let caps = re |
| 539 |
.captures_input(RegexInput::new("foo").start_text(true).end_text(true)) |
| 540 |
.unwrap() |
| 541 |
.unwrap(); |
| 542 |
assert_match(caps.get(0), "foo", 0, 3); |
| 543 |
assert_match(caps.get(1), "foo", 0, 3); |
| 544 |
|
| 545 |
let caps = re_bytes |
| 546 |
.captures_input( |
| 547 |
RegexInput::new("foo".as_bytes()) |
| 548 |
.start_text(true) |
| 549 |
.end_text(true), |
| 550 |
) |
| 551 |
.unwrap() |
| 552 |
.unwrap(); |
| 553 |
assert_eq!(caps.get(0).map(|m| (m.start(), m.end())), Some((0, 3))); |
| 554 |
assert_eq!(caps.get(1).map(|m| (m.start(), m.end())), Some((0, 3))); |
| 555 |
} |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn captures_iter_input_respects_range_for_empty_matches() { |
| 559 |
let regex = common::regex(r"(?m)(^)"); |
| 560 |
let text = "a\nb\n"; |
| 561 |
let spans: Vec<_> = regex |
| 562 |
.captures_iter_input(RegexInput::new(text).range(2..4)) |
| 563 |
.map(|caps| { |
| 564 |
let caps = caps.unwrap(); |
| 565 |
let m = caps.get(0).unwrap(); |
| 566 |
(m.start(), m.end()) |
| 567 |
}) |
| 568 |
.collect(); |
| 569 |
assert_eq!(spans, vec![(2, 2), (4, 4)]); |
| 570 |
} |
| 571 |
|
| 572 |
#[test] |
| 573 |
fn captures_iter_collect_when_backtrack_limit_hit() { |
| 574 |
use fancy_regex::{BytesMode, RegexBuilder}; |
| 575 |
let r = RegexBuilder::new("(x+x+)+(?>y)") |
| 576 |
.backtrack_limit(1) |
| 577 |
.build() |
| 578 |
.unwrap(); |
| 579 |
let result: Vec<_> = r.captures_iter("xxxxxxxxxxy").collect(); |
| 580 |
println!("{:?}", result); |
| 581 |
assert_eq!(result.len(), 1); |
| 582 |
assert!(result[0].is_err()); |
| 583 |
|
| 584 |
// The same behaviour must hold in bytes mode. |
| 585 |
let r = RegexBuilder::new("(x+x+)+(?>y)") |
| 586 |
.bytes_mode(BytesMode::Ascii) |
| 587 |
.backtrack_limit(1) |
| 588 |
.build() |
| 589 |
.unwrap(); |
| 590 |
let result: Vec<_> = r.captures_iter(b"xxxxxxxxxxy").collect(); |
| 591 |
assert_eq!(result.len(), 1); |
| 592 |
assert!(result[0].is_err()); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn captures_in_lookbehind_subroutine_call() { |
| 597 |
let captures = captures(r"(?<=(?<A>.)\g<A>)b", "axyabc"); |
| 598 |
|
| 599 |
assert_eq!(captures.len(), 2); |
| 600 |
assert_match(captures.get(0), "b", 4, 5); |
| 601 |
|
| 602 |
// right most capture takes precedence |
| 603 |
assert_match(captures.get(1), "a", 3, 4); |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
#[cfg(feature = "variable-lookbehinds")] |
| 608 |
fn captures_in_variable_lookbehind_subroutine_call() { |
| 609 |
let regex = common::regex(r"(?<=(?<A>[^b])(?<B>b+)\g<A>)d"); |
| 610 |
|
| 611 |
let captures = assert_captures(regex.captures_from_pos("aaabbcd", 5)); |
| 612 |
assert_eq!(captures.len(), 3); |
| 613 |
assert_match(captures.get(0), "d", 6, 7); |
| 614 |
assert_match(captures.get(2), "bb", 3, 5); |
| 615 |
|
| 616 |
// right most capture takes precedence |
| 617 |
assert_match(captures.get(1), "c", 5, 6); |
| 618 |
} |
| 619 |
|
| 620 |
#[test] |
| 621 |
fn self_recursive_capture_groups() { |
| 622 |
// Test recursive pattern with capture groups |
| 623 |
let caps1 = captures(r"(?<foo>a|\(\g<foo>\))", "((((((a))))))"); |
| 624 |
|
| 625 |
// According to Oniguruma behavior, capture group 1 should match the same as group 0 |
| 626 |
// for the input "((((((a))))))" |
| 627 |
assert_eq!(caps1.len(), 2); |
| 628 |
assert_match(caps1.get(0), "((((((a))))))", 0, 13); |
| 629 |
assert_match(caps1.get(1), "((((((a))))))", 0, 13); |
| 630 |
|
| 631 |
// Test with simpler input "(a)" |
| 632 |
let caps2 = captures(r"(?<foo>a|\(\g<foo>\))", "(a)"); |
| 633 |
assert_eq!(caps2.len(), 2); |
| 634 |
assert_match(caps2.get(0), "(a)", 0, 3); |
| 635 |
assert_match(caps2.get(1), "(a)", 0, 3); |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn forward_reference_subroutine_capture_groups() { |
| 640 |
// Test that a subroutine call updates the capture group |
| 641 |
let captures = captures(r"\g<_B>\g<_B>|\zEND(?<_A>.a.)(?<_B>.b.)", "xbxyby"); |
| 642 |
|
| 643 |
assert_eq!(captures.len(), 3); |
| 644 |
assert_match(captures.get(0), "xbxyby", 0, 6); |
| 645 |
assert!(captures.name("_A").is_none()); |
| 646 |
assert_match(captures.name("_B"), "yby", 3, 6); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn captures_easy() { |
| 651 |
let caps = common::assert_captures(r"(\d+)-(\d+)", "abc 123-456 def").unwrap(); |
| 652 |
assert_eq!(caps.len(), 3); |
| 653 |
assert_match(caps.get(0), "123-456", 4, 11); |
| 654 |
assert_match(caps.get(1), "123", 4, 7); |
| 655 |
assert_match(caps.get(2), "456", 8, 11); |
| 656 |
assert!(caps.get(3).is_none()); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn captures_named() { |
| 661 |
let caps = common::assert_captures(r"(?<first>\d+)-(?<second>\d+)", "12-34").unwrap(); |
| 662 |
assert_match(caps.name("first"), "12", 0, 2); |
| 663 |
assert_match(caps.name("second"), "34", 3, 5); |
| 664 |
assert!(caps.name("nonexistent").is_none()); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn captures_iter_groups() { |
| 669 |
let caps = common::assert_captures(r"(\d+)-(\d+)", "123-456").unwrap(); |
| 670 |
let groups: Vec<_> = caps.iter().collect(); |
| 671 |
assert_eq!(groups.len(), 3); |
| 672 |
assert_match(groups[0], "123-456", 0, 7); |
| 673 |
assert_match(groups[1], "123", 0, 3); |
| 674 |
assert_match(groups[2], "456", 4, 7); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn captures_backrefs() { |
| 679 |
let caps = common::assert_captures(r"(\w+)\s+\1", "abc abc").unwrap(); |
| 680 |
assert_eq!(caps.len(), 2); |
| 681 |
assert_match(caps.get(0), "abc abc", 0, 7); |
| 682 |
assert_match(caps.get(1), "abc", 0, 3); |
| 683 |
} |
| 684 |
|
| 685 |
#[test] |
| 686 |
fn captures_subroutine() { |
| 687 |
let caps = common::assert_captures(r"^(?<pair>..)\g<pair>$", "abcd").unwrap(); |
| 688 |
assert_eq!(caps.len(), 2); |
| 689 |
assert_match(caps.get(0), "abcd", 0, 4); |
| 690 |
assert_match(caps.name("pair"), "cd", 2, 4); |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn captures_lookbehind_digits() { |
| 695 |
let caps = common::assert_captures(r"(?<=[a-z])(\d+)", "abc123").unwrap(); |
| 696 |
assert_eq!(caps.len(), 2); |
| 697 |
assert_match(caps.get(0), "123", 3, 6); |
| 698 |
assert_match(caps.get(1), "123", 3, 6); |
| 699 |
} |
| 700 |
|
| 701 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 702 |
fn captures<'a>(re: &str, text: &'a str) -> Captures<'a, str> { |
| 703 |
let regex = common::regex(re); |
| 704 |
let result = regex.captures(text); |
| 705 |
assert_captures(result) |
| 706 |
} |
| 707 |
|
| 708 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 709 |
fn assert_captures(result: Result<Option<Captures<'_, str>>>) -> Captures<'_, str> { |
| 710 |
assert!( |
| 711 |
result.is_ok(), |
| 712 |
"Expected captures to succeed, but was {:?}", |
| 713 |
result |
| 714 |
); |
| 715 |
let captures = result.unwrap(); |
| 716 |
assert!( |
| 717 |
captures.is_some(), |
| 718 |
"Expected captures, but was {:?}", |
| 719 |
captures |
| 720 |
); |
| 721 |
captures.unwrap() |
| 722 |
} |
| 723 |
|
| 724 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 725 |
fn assert_match(m: Option<Match<'_>>, expected_text: &str, start: usize, end: usize) { |
| 726 |
assert!(m.is_some(), "Expected match, but was {:?}", m); |
| 727 |
let m = m.unwrap(); |
| 728 |
assert_eq!(m.as_str(), expected_text); |
| 729 |
assert_eq!(m.start(), start); |
| 730 |
assert_eq!(m.end(), end); |
| 731 |
} |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn expand() { |
| 735 |
let regex = common::regex("(a)(b)(?<π>c)(?P<x>d)"); |
| 736 |
let cap = regex.captures("abcd").unwrap().expect("matched"); |
| 737 |
assert_expansion(&cap, "$0", "abcd"); |
| 738 |
assert_expansion(&cap, "$1", "a"); |
| 739 |
assert_expansion(&cap, "$2", "b"); |
| 740 |
assert_expansion(&cap, "$3", "c"); |
| 741 |
assert_expansion(&cap, "$4", "d"); |
| 742 |
assert_expansion(&cap, "$π", "c"); |
| 743 |
assert_expansion(&cap, "$x", "d"); |
| 744 |
assert_expansion(&cap, "$0π", ""); |
| 745 |
assert_expansion(&cap, "$1π", ""); |
| 746 |
assert_expansion(&cap, "$2π", ""); |
| 747 |
assert_expansion(&cap, "$3π", ""); |
| 748 |
assert_expansion(&cap, "$4π", ""); |
| 749 |
assert_expansion(&cap, "$ππ", ""); |
| 750 |
assert_expansion(&cap, "$xπ", ""); |
| 751 |
assert_expansion(&cap, "${0}π", "abcdπ"); |
| 752 |
assert_expansion(&cap, "${1}π", "aπ"); |
| 753 |
assert_expansion(&cap, "${2}π", "bπ"); |
| 754 |
assert_expansion(&cap, "${3}π", "cπ"); |
| 755 |
assert_expansion(&cap, "${4}π", "dπ"); |
| 756 |
assert_expansion(&cap, "${π}π", "cπ"); |
| 757 |
assert_expansion(&cap, "${x}π", "dπ"); |
| 758 |
assert_expansion(&cap, "$", "$"); |
| 759 |
assert_expansion(&cap, "$π√", "c√"); |
| 760 |
assert_expansion(&cap, "$x√", "d√"); |
| 761 |
assert_expansion(&cap, "$$π", "$π"); |
| 762 |
assert_expansion(&cap, "${π", "${π"); |
| 763 |
assert_python_expansion(&cap, "\\0", "abcd"); |
| 764 |
assert_python_expansion(&cap, "\\1", "a"); |
| 765 |
assert_python_expansion(&cap, "\\2", "b"); |
| 766 |
assert_python_expansion(&cap, "\\3", "c"); |
| 767 |
assert_python_expansion(&cap, "\\4", "d"); |
| 768 |
assert_python_expansion(&cap, "\\π", "\\π"); |
| 769 |
assert_python_expansion(&cap, "\\x", "\\x"); |
| 770 |
assert_python_expansion(&cap, "\\0π", "abcdπ"); |
| 771 |
assert_python_expansion(&cap, "\\1π", "aπ"); |
| 772 |
assert_python_expansion(&cap, "\\2π", "bπ"); |
| 773 |
assert_python_expansion(&cap, "\\3π", "cπ"); |
| 774 |
assert_python_expansion(&cap, "\\4π", "dπ"); |
| 775 |
assert_python_expansion(&cap, "\\ππ", "\\ππ"); |
| 776 |
assert_python_expansion(&cap, "\\xπ", "\\xπ"); |
| 777 |
assert_python_expansion(&cap, "\\g<0>π", "abcdπ"); |
| 778 |
assert_python_expansion(&cap, "\\g<1>π", "aπ"); |
| 779 |
assert_python_expansion(&cap, "\\g<2>π", "bπ"); |
| 780 |
assert_python_expansion(&cap, "\\g<3>π", "cπ"); |
| 781 |
assert_python_expansion(&cap, "\\g<4>π", "dπ"); |
| 782 |
assert_python_expansion(&cap, "\\g<π>π", "cπ"); |
| 783 |
assert_python_expansion(&cap, "\\g<x>π", "dπ"); |
| 784 |
assert_python_expansion(&cap, "\\", "\\"); |
| 785 |
assert_python_expansion(&cap, "\\\\π", "\\π"); |
| 786 |
assert_python_expansion(&cap, "\\g<π", "\\g<π"); |
| 787 |
} |
| 788 |
|
| 789 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 790 |
fn assert_expansion(cap: &Captures<'_, str>, replacement: &str, text: &str) { |
| 791 |
let mut buf = "before".to_string(); |
| 792 |
cap.expand(replacement, &mut buf); |
| 793 |
assert_eq!(buf, format!("before{}", text)); |
| 794 |
} |
| 795 |
|
| 796 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 797 |
fn assert_python_expansion(cap: &Captures<'_, str>, replacement: &str, text: &str) { |
| 798 |
assert_eq!(Expander::python().expansion(replacement, cap), text); |
| 799 |
} |
| 800 |
|
| 801 |
#[test] |
| 802 |
fn expander_escape() { |
| 803 |
match Expander::default().escape("hello") { |
| 804 |
Cow::Borrowed(s) => assert_eq!(s, "hello"), |
| 805 |
_ => panic!("string should be borrowed"), |
| 806 |
} |
| 807 |
assert_eq!(Expander::default().escape("a$b\\c"), "a$$b\\c"); |
| 808 |
assert_eq!(Expander::python().escape("a$b\\c"), "a$b\\\\c"); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
fn expander_errors() { |
| 813 |
let with_names = common::regex("(?<x>a)"); |
| 814 |
let without_names = common::regex("(a)"); |
| 815 |
let exp = Expander::default(); |
| 816 |
|
| 817 |
macro_rules! assert_err { |
| 818 |
($expr:expr, $err:pat) => { |
| 819 |
match $expr { |
| 820 |
Err($err) => {} |
| 821 |
x => panic!("wrong result: {:?}", x), |
| 822 |
} |
| 823 |
}; |
| 824 |
} |
| 825 |
|
| 826 |
// Substitution char at end of template. |
| 827 |
assert_err!(exp.check("$", &with_names), Error::ParseError(_, _)); |
| 828 |
|
| 829 |
// Substitution char not followed by a name or number. |
| 830 |
assert_err!(exp.check("$.", &with_names), Error::ParseError(_, _)); |
| 831 |
|
| 832 |
// Empty delimiter pair. |
| 833 |
assert_err!(exp.check("${}", &with_names), Error::ParseError(_, _)); |
| 834 |
|
| 835 |
// Unterminated delimiter pair. |
| 836 |
assert_err!(exp.check("${", &with_names), Error::ParseError(_, _)); |
| 837 |
|
| 838 |
// Group 0 is always OK. |
| 839 |
assert!(exp.check("$0", &with_names).is_ok()); |
| 840 |
assert!(exp.check("$0", &without_names).is_ok()); |
| 841 |
|
| 842 |
// Can't use numbers with named groups. |
| 843 |
assert!(matches!( |
| 844 |
exp.check("$1", &with_names), |
| 845 |
Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::NamedBackrefOnly) |
| 846 |
)); |
| 847 |
assert!(matches!( |
| 848 |
exp.check("${1}", &with_names), |
| 849 |
Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::NamedBackrefOnly) |
| 850 |
)); |
| 851 |
|
| 852 |
// Unmatched group number. |
| 853 |
assert!(matches!( |
| 854 |
exp.check("$2", &without_names), |
| 855 |
Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::InvalidBackref(2)) |
| 856 |
)); |
| 857 |
assert!(matches!( |
| 858 |
exp.check("${2}", &without_names), |
| 859 |
Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::InvalidBackref(2)) |
| 860 |
)); |
| 861 |
|
| 862 |
// Unmatched group name. |
| 863 |
assert!( |
| 864 |
matches!(exp.check("$xx", &with_names), Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::InvalidGroupNameBackref(ref name) if name == "xx")), |
| 865 |
); |
| 866 |
assert!(matches!( |
| 867 |
exp.check("${xx}", &with_names), |
| 868 |
Err(Error::CompileError(ref box_err)) if matches!(**box_err, CompileError::InvalidGroupNameBackref(ref name) if name == "xx"))); |
| 869 |
} |
| 870 |
|
| 871 |
#[test] |
| 872 |
fn absent_repeater_hard_captures_opening_fence() { |
| 873 |
// Matches a markdown code fence where the opening backticks are captured in group 1, |
| 874 |
// then the body is matched while those backticks are absent, then the same backticks close. |
| 875 |
// Pattern: (`{3,})(?~\1)\1 - capture 3+ backticks, absent-repeat until those backticks, |
| 876 |
// then match the same backticks to close. |
| 877 |
let re = common::regex(r"(`{3,})(?~\1)\1"); |
| 878 |
|
| 879 |
// Three-backtick fence |
| 880 |
let cap = re.captures("```code```").unwrap().unwrap(); |
| 881 |
assert_match(cap.get(0), "```code```", 0, 10); |
| 882 |
assert_match(cap.get(1), "```", 0, 3); |
| 883 |
|
| 884 |
// Four-backtick fence - allows backticks inside |
| 885 |
let cap = re.captures("````has ``` inside````").unwrap().unwrap(); |
| 886 |
assert_match(cap.get(0), "````has ``` inside````", 0, 22); |
| 887 |
assert_match(cap.get(1), "````", 0, 4); |
| 888 |
|
| 889 |
// Three backticks should match the shorter fence and stop at the first ``` occurrence |
| 890 |
let cap = re |
| 891 |
.captures("```first``` and ```second```") |
| 892 |
.unwrap() |
| 893 |
.unwrap(); |
| 894 |
assert_match(cap.get(0), "```first```", 0, 11); |
| 895 |
assert_match(cap.get(1), "```", 0, 3); |
| 896 |
} |
| 897 |
|
| 898 |
#[test] |
| 899 |
fn captures_with_unrestricted_group_names() { |
| 900 |
// Hyphenated group name |
| 901 |
let regex = common::regex(r"(?<foo-bar>\w+)"); |
| 902 |
let cap = regex.captures("hello").unwrap().expect("matched"); |
| 903 |
let m = cap.name("foo-bar").expect("group found"); |
| 904 |
assert_eq!(m.as_str(), "hello"); |
| 905 |
|
| 906 |
// CSS-style name |
| 907 |
let regex = common::regex(r"(?<data-value>\d+)"); |
| 908 |
let cap = regex.captures("42").unwrap().expect("matched"); |
| 909 |
let m = cap.name("data-value").expect("group found"); |
| 910 |
assert_eq!(m.as_str(), "42"); |
| 911 |
|
| 912 |
// Python syntax with hyphen |
| 913 |
let regex = common::regex(r"(?P<my-group>[a-z]+)"); |
| 914 |
let cap = regex.captures("hello").unwrap().expect("matched"); |
| 915 |
let m = cap.name("my-group").expect("group found"); |
| 916 |
assert_eq!(m.as_str(), "hello"); |
| 917 |
|
| 918 |
// Single-quote syntax with hyphen |
| 919 |
let regex = common::regex(r"(?'my-group'[a-z]+)"); |
| 920 |
let cap = regex.captures("hello").unwrap().expect("matched"); |
| 921 |
let m = cap.name("my-group").expect("group found"); |
| 922 |
assert_eq!(m.as_str(), "hello"); |
| 923 |
|
| 924 |
// Name with special characters |
| 925 |
let regex = common::regex(r"(?<#tag>\w+)"); |
| 926 |
let cap = regex.captures("hello").unwrap().expect("matched"); |
| 927 |
let m = cap.name("#tag").expect("group found"); |
| 928 |
assert_eq!(m.as_str(), "hello"); |
| 929 |
|
| 930 |
// Multiple unrestricted groups |
| 931 |
let regex = common::regex(r"(?<first-name>\w+)\s+(?<last-name>\w+)"); |
| 932 |
let cap = regex.captures("John Doe").unwrap().expect("matched"); |
| 933 |
assert_eq!(cap.name("first-name").unwrap().as_str(), "John"); |
| 934 |
assert_eq!(cap.name("last-name").unwrap().as_str(), "Doe"); |
| 935 |
} |
| 936 |
|
| 937 |
#[test] |
| 938 |
fn capture_names_with_unrestricted_group_names() { |
| 939 |
let regex = common::regex(r"(?<foo-bar>\w+)\s+(?<baz-qux>\w+)"); |
| 940 |
let names: Vec<_> = regex.capture_names().collect(); |
| 941 |
assert_eq!(names[0], None); // group 0 is unnamed |
| 942 |
// The order of named groups is: foo-bar=1, baz-qux=2 |
| 943 |
assert!(names.contains(&Some("foo-bar"))); |
| 944 |
assert!(names.contains(&Some("baz-qux"))); |
| 945 |
} |