pub fn regexec<'a>(
compiled_reg: &Regex,
string: &'a str,
nmatches: usize,
flags: RegexecFlags,
) -> Result<RegMatchStr<'a>>
Expand description
Performs a regex search on the passed string, returning nmatches
results.
This is a thin wrapper around Regex::regexec
.
Non-matching subexpressions or patterns will return None
in the results.
§Arguments
compiled_reg
: the compiledRegex
object.string
: string to match againstcompiled_reg
nmatches
: number of matches to returnflags
:RegexecFlags
to pass totre_regnexec
.
§Returns
If no error was found, a Vec
of Option
s will be returned.
If a given match index is empty, The Option
will be None
. Otherwise, Result
s will be
returned, containing either errors or substrings of the matches. Errors may be returned due to
decoding problems, such as split codepoints.
§Errors
If an error is encountered during matching, it returns a RegexError
. Match results may also
return errors, if decoding into UTF-8 was unsuccessful for whatever reason.
§Caveats
Unless copied, the match results must live at least as long as string
. This is because they are
slices into string
under the hood, for efficiency.
§Examples
use tre_regex::{RegcompFlags, RegexecFlags, regcomp, regexec};
let regcomp_flags = RegcompFlags::new()
.add(RegcompFlags::EXTENDED)
.add(RegcompFlags::ICASE)
.add(RegcompFlags::UNGREEDY);
let regexec_flags = RegexecFlags::new().add(RegexecFlags::NONE);
let compiled_reg = regcomp("^(hello).*(world)$", regcomp_flags)?;
let matches = regexec(
&compiled_reg, // Compiled regex
"hello world", // String to match against
2, // Number of matches
regexec_flags // Flags
)?;
for (i, matched) in matches.into_iter().enumerate() {
match matched {
Some(substr) => {
match substr {
Ok(substr) => println!("Match {i}: '{}'", substr),
Err(e) => println!("Match {i}: <Error: {e}>"),
}
},
None => println!("Match {i}: <None>"),
}
}