pub struct FuzzyRegex { /* private fields */ }Expand description
A compiled fuzzy regular expression.
§Example
use fuzzy_regex::FuzzyRegex;
let re = FuzzyRegex::new(r"hello~2").unwrap();
assert!(re.is_match("helo")); // Matches with 1 edit
assert!(re.is_match("hello")); // Exact matchImplementations§
Source§impl FuzzyRegex
impl FuzzyRegex
Sourcepub fn new(pattern: &str) -> Result<Self>
pub fn new(pattern: &str) -> Result<Self>
Create a new FuzzyRegex with default settings.
For customized settings, use FuzzyRegexBuilder.
§Errors
Returns an error if the pattern is invalid or cannot be compiled.
Sourcepub fn builder(pattern: &str) -> FuzzyRegexBuilder
pub fn builder(pattern: &str) -> FuzzyRegexBuilder
Create a builder for customized regex construction.
Sourcepub fn captures_len(&self) -> usize
pub fn captures_len(&self) -> usize
Get the number of capture groups.
Sourcepub fn similarity_threshold(&self) -> f32
pub fn similarity_threshold(&self) -> f32
Get the configured similarity threshold.
Sourcepub fn literals(&self) -> &[LiteralPattern]
pub fn literals(&self) -> &[LiteralPattern]
Get the literal patterns extracted from this regex.
This is useful for debugging and introspection.
Sourcepub fn is_simple_fuzzy(&self) -> bool
pub fn is_simple_fuzzy(&self) -> bool
Check if this pattern is detected as “simple” (single fuzzy literal). Simple patterns can skip NFA simulation for faster matching.
Sourcepub fn set_word_list(
&mut self,
name: impl Into<SmartCow<'static>>,
words: Vec<impl Into<SmartCow<'static>>>,
)
pub fn set_word_list( &mut self, name: impl Into<SmartCow<'static>>, words: Vec<impl Into<SmartCow<'static>>>, )
Set a named word list for \L
§Example
let mut re = fuzzy_regex::FuzzyRegex::new(r"\b\L<words>{e<=1}\b").unwrap();
re.set_word_list("words", vec!["cat", "dog", "frog"]);
assert!(re.is_match("cot")); // 1 substitution from "cat"
assert!(re.is_match("dag")); // 1 substitution from "dog")Sourcepub fn get_word_list(&self, name: &str) -> Option<&[SmartCow<'static>]>
pub fn get_word_list(&self, name: &str) -> Option<&[SmartCow<'static>]>
Get a named word list.
Sourcepub fn named_lists(&self) -> &HashMap<SmartCow<'static>, Vec<SmartCow<'static>>>
pub fn named_lists(&self) -> &HashMap<SmartCow<'static>, Vec<SmartCow<'static>>>
Get all named word lists.
Returns a reference to the internal word lists map.
This matches the API of mrab-regex’s named_lists property.
Sourcepub fn has_word_lists(&self) -> bool
pub fn has_word_lists(&self) -> bool
Check if this regex has any word lists defined.
Sourcepub fn is_match_at(&self, text: &str, start: usize) -> bool
pub fn is_match_at(&self, text: &str, start: usize) -> bool
Check if the pattern matches at the start of the text.
Sourcepub fn is_full_match(&self, text: &str) -> bool
pub fn is_full_match(&self, text: &str) -> bool
Check if the pattern matches the entire text.
This is equivalent to anchoring the pattern at both start and end.
Sourcepub fn fullmatch<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn fullmatch<'t>(&self, text: &'t str) -> Option<Match<'t>>
Find a match that spans the entire text.
Returns Some if the pattern matches the full string from start to end.
This is equivalent to using ^pattern$ in a regular expression.
Sourcepub fn fullmatch_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
pub fn fullmatch_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
Find a match that spans from the given start position to the end.
The match must start at start and end at text.len().
Sourcepub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>>
Find the first match in the text. In BESTMATCH mode, returns the match with fewest errors. In ENHANCEMATCH mode, improves the fit of the found match.
Sourcepub fn find_with_timeout<'t>(
&self,
text: &'t str,
timeout: Duration,
) -> Result<Option<Match<'t>>>
pub fn find_with_timeout<'t>( &self, text: &'t str, timeout: Duration, ) -> Result<Option<Match<'t>>>
Find the first match with a timeout.
Note: Timeout is checked at certain checkpoints during matching, so it’s not precise. The actual time may exceed the timeout slightly.
Sourcepub fn find_with_config_timeout<'t>(
&self,
text: &'t str,
) -> Result<Option<Match<'t>>>
pub fn find_with_config_timeout<'t>( &self, text: &'t str, ) -> Result<Option<Match<'t>>>
Find first match using config timeout (if set).
This uses the timeout configured via FuzzyRegexBuilder::timeout().
Sourcepub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
pub fn find_at<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
Find a match starting at exactly the given position.
This only matches if a match starts at exactly start. Use find_from
to search from start onwards.
The full text is passed to the matcher for proper boundary handling
(e.g., \b word boundaries need context from preceding characters).
Sourcepub fn find_from<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
pub fn find_from<'t>(&self, text: &'t str, start: usize) -> Option<Match<'t>>
Find the first match at or after the given position.
Unlike find_at which only matches at exactly start, this searches
forward from start until a match is found or the text is exhausted.
Sourcepub fn find_rev<'t>(&self, text: &'t str) -> Option<Match<'t>>
pub fn find_rev<'t>(&self, text: &'t str) -> Option<Match<'t>>
Find the last match in the text (reverse search).
This searches from the end of the text backwards, returning the rightmost match.
Similar to Python’s re.search() with a reversed pattern.
Sourcepub fn find_iter_rev<'t>(&self, text: &'t str) -> Vec<Match<'t>>
pub fn find_iter_rev<'t>(&self, text: &'t str) -> Vec<Match<'t>>
Find all matches from the end (reverse order).
Returns matches in reverse order (rightmost first).
Sourcepub fn find_n<'t>(&self, text: &'t str, n: usize) -> Vec<Match<'t>>
pub fn find_n<'t>(&self, text: &'t str, n: usize) -> Vec<Match<'t>>
Find the first n non-overlapping matches.
This is more efficient than find_iter().take(n).collect() because it
stops searching after finding n matches instead of collecting all matches first.
§Example
use fuzzy_regex::FuzzyRegex;
let re = FuzzyRegex::new(r"(?:test){e<=1}").unwrap();
let text = "test tset testing tests";
let first_two = re.find_n(text, 2);
assert_eq!(first_two.len(), 2);Sourcepub fn find_all_overlapping<'t>(&self, text: &'t str) -> Vec<Match<'t>>
pub fn find_all_overlapping<'t>(&self, text: &'t str) -> Vec<Match<'t>>
Find all matches, including overlapping ones.
Unlike find_iter, this method tries every position in the text
and returns all possible matches, even if they overlap.
Sourcepub fn find_all_overlapping_filtered<'t>(
&self,
text: &'t str,
similarity_threshold: f32,
) -> Vec<Match<'t>>
pub fn find_all_overlapping_filtered<'t>( &self, text: &'t str, similarity_threshold: f32, ) -> Vec<Match<'t>>
Find all matches above a similarity threshold, including overlapping ones.
This is more efficient than find_all_overlapping followed by filtering,
as it skips creating Match objects for results below the threshold.
Sourcepub fn captures_all_overlapping<'t>(
&self,
text: &'t str,
similarity_threshold: f32,
) -> Vec<Captures<'t>>
pub fn captures_all_overlapping<'t>( &self, text: &'t str, similarity_threshold: f32, ) -> Vec<Captures<'t>>
Get all overlapping matches with capture group information.
This is useful for identifying which alternative in an alternation matched.
Sourcepub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
Get captures for the first match.
Sourcepub fn captures_at<'t>(
&self,
text: &'t str,
start: usize,
) -> Option<Captures<'t>>
pub fn captures_at<'t>( &self, text: &'t str, start: usize, ) -> Option<Captures<'t>>
Get captures starting at a specific position.
Sourcepub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't> ⓘ
pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't> ⓘ
Iterate over all capture groups.
Sourcepub fn replace(&self, text: &str, replacement: &str) -> String
pub fn replace(&self, text: &str, replacement: &str) -> String
Replace the first match.
§Panics
This function should not panic. The internal unwrap() is safe because
a match result always contains the full match at index 0.
Sourcepub fn replace_all(&self, text: &str, replacement: &str) -> String
pub fn replace_all(&self, text: &str, replacement: &str) -> String
Replace all non-overlapping matches.
Sourcepub fn replace_all_with<F>(&self, text: &str, replacer: F) -> String
pub fn replace_all_with<F>(&self, text: &str, replacer: F) -> String
Replace matches using a closure.
Sourcepub fn splitn<'t>(&self, text: &'t str, n: usize) -> Vec<&'t str>
pub fn splitn<'t>(&self, text: &'t str, n: usize) -> Vec<&'t str>
Split the text into at most n parts.
This is more efficient than split().take(n).collect() because it
stops searching after finding enough splits.
The last element will contain the remainder of the string if there
are more than n-1 matches.
§Example
use fuzzy_regex::FuzzyRegex;
let re = FuzzyRegex::new(r",").unwrap();
let parts = re.splitn("a,b,c,d,e", 3);
assert_eq!(parts, vec!["a", "b", "c,d,e"]);Sourcepub fn stream(&self) -> StreamingMatcher<'_>
pub fn stream(&self) -> StreamingMatcher<'_>
Create a streaming matcher for incremental processing.
This allows processing large files or network streams without loading everything into memory. Matches can span chunk boundaries.
§Example
use fuzzy_regex::FuzzyRegex;
let re = FuzzyRegex::new("(?:hello){e<=1}").unwrap();
let mut stream = re.stream();
// Process data in chunks
for m in stream.feed(b"hel") {
println!("Match at {}", m.start());
}
for m in stream.feed(b"lo world") {
println!("Match at {}", m.start());
}Sourcepub fn is_match_bytes(&self, text: &[u8]) -> bool
pub fn is_match_bytes(&self, text: &[u8]) -> bool
Check if a pattern matches anywhere in the byte slice.
This is similar to is_match but works with &[u8] instead of &str.
Sourcepub fn find_bytes(&self, text: &[u8]) -> Option<StreamingMatch>
pub fn find_bytes(&self, text: &[u8]) -> Option<StreamingMatch>
Find the first match in a byte slice.
Returns a StreamingMatch with byte offsets.
Sourcepub fn find_iter_bytes<'r, 't>(&'r self, text: &'t [u8]) -> ByteMatches<'r, 't> ⓘ
pub fn find_iter_bytes<'r, 't>(&'r self, text: &'t [u8]) -> ByteMatches<'r, 't> ⓘ
Find all non-overlapping matches in a byte slice.
Returns an iterator over StreamingMatch objects.
Sourcepub fn supports_streaming(&self) -> bool
pub fn supports_streaming(&self) -> bool
Check if this pattern supports fast streaming search.
Returns true if the pattern can use the optimized Bitap-based
streaming algorithm (pattern length <= 64 characters).