find

Function find 

Source
pub fn find<C: Cursor>(regex: &Regex, input: &mut Input<C>) -> Option<Match>
Expand description

Returns the start and end offset of the leftmost match. If no match exists, then None is returned.

§Panics

This routine panics if the search could not complete. This can occur in a number of circumstances:

  • The configuration of the lazy DFA may permit it to “quit” the search. For example, setting quit bytes or enabling heuristic support for Unicode word boundaries. The default configuration does not enable any option that could result in the lazy DFA quitting.
  • The configuration of the lazy DFA may also permit it to “give up” on a search if it makes ineffective use of its transition table cache. The default configuration does not enable this by default, although it is typically a good idea to.
  • When the provided Input configuration is not supported. For example, by providing an unsupported anchor mode.

When a search panics, callers cannot know whether a match exists or not.

Use Regex::try_search if you want to handle these error conditions.

§Example

use regex_automata::{Match, hybrid::regex::Regex};

let re = Regex::new("foo[0-9]+")?;
let mut cache = re.create_cache();
assert_eq!(
    Some(Match::must(0, 3..11)),
    re.find(&mut cache, "zzzfoo12345zzz"),
);

// Even though a match is found after reading the first byte (`a`),
// the default leftmost-first match semantics demand that we find the
// earliest match that prefers earlier parts of the pattern over latter
// parts.
let re = Regex::new("abc|a")?;
let mut cache = re.create_cache();
assert_eq!(Some(Match::must(0, 0..3)), re.find(&mut cache, "abc"));