find_pattern

Function find_pattern 

Source
pub fn find_pattern(haystack: &[u8], needle: &Regex) -> Option<(usize, usize)>
Expand description

Find a regex pattern in a byte slice.

This is a utility function that uses regex to find patterns in byte data. It’s used internally by the streaming functions but can also be used directly for in-memory pattern matching.

§Arguments

  • haystack - The byte slice to search in
  • needle - The compiled regex pattern to search for

§Returns

Returns Some((start, length)) if the pattern is found, None otherwise.

§Examples

use async_regex::find_pattern;
use regex::bytes::Regex;
 
let regex = Regex::new(r"\w+").unwrap();
let data = b"hello world";
 
if let Some((start, len)) = find_pattern(data, &regex) {
    println!("Found pattern at position {} with length {}", start, len);
}