Function tre_regex::regexec_bytes
source · pub fn regexec_bytes<'a>(
compiled_reg: &Regex,
data: &'a [u8],
nmatches: usize,
flags: RegexecFlags
) -> Result<RegMatchBytes<'a>>Expand description
Performs a regex search on the passed bytes, returning nmatches results.
This is a thin wrapper around Regex::regexec_bytes.
This function should only be used if you need to match raw bytes, or bytes which may not be
UTF-8 compliant. Otherwise, regexec is recommended instead.
Arguments
compiled_reg: the compiledRegexobject.data:u8slice to match againstcompiled_regnmatches: number of matches to returnflags:RegexecFlagsto pass totre_regnexec.
Returns
If no error was found, a Vec of Options will be returned.
If a given match index is empty, The Option will be None. Otherwise, u8 slices will be
returned.
Errors
If an error is encountered during matching, it returns a RegexError.
Caveats
Unless copied, the match results must live at least as long as data. This is because they are
slices into data under the hood, for efficiency.
Examples
use tre_regex::{RegcompFlags, RegexecFlags, regcomp, regexec_bytes};
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_bytes(
&compiled_reg, // Compiled regex
b"hello world", // Bytes to match against
2, // Number of matches
regexec_flags // Flags
)?;
for (i, matched) in matches.into_iter().enumerate() {
match matched {
Some(substr) => println!(
"Match {i}: {}",
std::str::from_utf8(substr.as_ref()).unwrap()
),
None => println!("Match {i}: <None>"),
}
}