Single match in the requested mode. */
(pattern: &str, text: &str, mode: Mode)
| 46 | |
| 47 | /* Single match in the requested mode. */ |
| 48 | pub fn find(pattern: &str, text: &str, mode: Mode) -> Result<Option<Found>, ReError> { |
| 49 | let re = Regex::compile(pattern)?; |
| 50 | let chars: Vec<char> = text.chars().collect(); |
| 51 | let m = Matcher::new(&chars, re.prog.flags); |
| 52 | let caps = match mode { |
| 53 | Mode::Search => m.search(&re.prog.root, re.prog.group_count), |
| 54 | Mode::Match => m.match_at(&re.prog.root, re.prog.group_count, false), |
| 55 | Mode::Full => m.match_at(&re.prog.root, re.prog.group_count, true), |
| 56 | }; |
| 57 | match caps { |
| 58 | Some(c) => Ok(Some(build(&chars, &c, re.prog.group_count))), |
| 59 | None if m.exceeded() => Err(too_complex()), |
| 60 | None => Ok(None), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* All non overlapping matches, plus the group count for output shaping. */ |
| 65 | pub fn find_all(pattern: &str, text: &str) -> Result<(Vec<Found>, usize), ReError> { |