Skip to main content

Crate fuzzy_regex

Crate fuzzy_regex 

Source
Expand description

§Fuzzy Regex

A fuzzy regular expression engine that combines traditional regex constructs with fuzzy (approximate) string matching using Damerau-Levenshtein automata.

§Features

  • Fuzzy literal matching: Match strings with edit distance tolerance
  • Full regex support: Character classes, quantifiers, groups, alternation, anchors
  • Per-segment fuzziness: Control fuzziness for individual parts of a pattern
  • Capture groups: Named and numbered capture groups
  • Similarity scoring: Get match quality scores (0.0 - 1.0)

§Quick Start

use fuzzy_regex::FuzzyRegex;

// Simple fuzzy matching - hello~2 allows up to 2 edits
let re = FuzzyRegex::builder("hello~2")
    .similarity(0.7)
    .build()
    .unwrap();

assert!(re.is_match("hello"));  // Exact match
assert!(re.is_match("helo"));   // 1 deletion
assert!(re.is_match("helllo")); // 1 insertion

§Pattern Syntax

§Fuzziness Markers

  • hello~2 - Allow up to 2 edits in “hello”
  • hello~0 - Exact match only (no edits)
  • hello~{i=1,d=2,s=0} - Detailed limits: 1 insertion, 2 deletions, 0 substitutions
  • (hello world)~2 - Fuzziness on entire group

§Standard Regex Constructs

  • [a-z], \d, \w, \s - Character classes
  • *, +, ?, {n,m} - Quantifiers
  • (group), (?:non-capture) - Groups
  • (?<name>...) - Named capture groups
  • a|b - Alternation
  • ^, $ - Anchors
  • (?=...), (?!...) - Lookahead

§Examples

use fuzzy_regex::FuzzyRegexBuilder;

let re = FuzzyRegexBuilder::new(r"color~2|colour~2")
    .similarity(0.8)
    .build()
    .unwrap();

// Matches various spellings
assert!(re.is_match("color"));
assert!(re.is_match("colour"));
assert!(re.is_match("colur")); // With typo

§Capture Groups with Fuzzy Matching

use fuzzy_regex::FuzzyRegex;

let re = FuzzyRegex::new(r"(?<word>\w+)").unwrap();
let caps = re.captures("hello").unwrap();
assert_eq!(caps.name("word").unwrap().as_str(), "hello");

§Replace with Fuzzy Matching

use fuzzy_regex::FuzzyRegexBuilder;

// Match "recieve" (common misspelling) and replace with correct spelling
// Note: fuzzy matching allows edits including insertions/deletions at boundaries,
// so we use exact match here (~0) to replace just the misspelled word
let re = FuzzyRegexBuilder::new(r"recieve~0")
    .build()
    .unwrap();

let result = re.replace("Did you recieve the package?", "receive");
assert_eq!(result, "Did you receive the package?");

Re-exports§

pub use api::ByteMatches;
pub use api::CaptureMatches;
pub use api::Captures;
pub use api::FeedMatches;
pub use api::FuzzyRegex;
pub use api::FuzzyRegexBuilder;
pub use api::Match;
pub use api::MatchFlags;
pub use api::Matches;
pub use api::ReaderMatches;
pub use api::RegexConfig;
pub use api::Split;
pub use api::StreamingMatch;
pub use api::StreamingMatcher;
pub use engine::EditCounts;
pub use error::Error;
pub use error::Result;
pub use types::FuzzyLimits;
pub use types::FuzzyPenalties;

Modules§

api
Public API module.
compat
Compatibility layer for fuzzy-aho-corasick API.
compiler
Compiler for converting parsed regex AST into executable IR. Compiler module for transforming HIR to NFA.
engine
Core matching engines including NFA simulation and Bitap algorithm. Matching engine module.
error
Error types for the fuzzy regex engine.
ir
Intermediate representation for compiled regex patterns. Intermediate Representation module.
parser
Regex pattern parser and lexer. Parser module for fuzzy regex patterns.
types
Core types for fuzzy matching limits and penalties.