Skip to main content

fuzzy_regex/
lib.rs

1#![warn(clippy::pedantic)]
2#![allow(
3    clippy::let_underscore_untyped,
4    clippy::struct_excessive_bools,
5    clippy::float_cmp,
6    let_underscore_drop,
7    clippy::cast_possible_truncation,
8    clippy::cast_precision_loss,
9    clippy::cast_sign_loss,
10    clippy::cast_possible_wrap,
11    clippy::too_many_lines,
12    clippy::cast_lossless,
13    clippy::cast_ptr_alignment,
14    clippy::ptr_as_ptr
15)]
16
17//! # Fuzzy Regex
18//!
19//! A fuzzy regular expression engine that combines traditional regex constructs
20//! with fuzzy (approximate) string matching using Damerau-Levenshtein automata.
21//!
22//! ## Features
23//!
24//! - **Fuzzy literal matching**: Match strings with edit distance tolerance
25//! - **Full regex support**: Character classes, quantifiers, groups, alternation, anchors
26//! - **Per-segment fuzziness**: Control fuzziness for individual parts of a pattern
27//! - **Capture groups**: Named and numbered capture groups
28//! - **Similarity scoring**: Get match quality scores (0.0 - 1.0)
29//!
30//! ## Quick Start
31//!
32//! ```rust
33//! use fuzzy_regex::FuzzyRegex;
34//!
35//! // Simple fuzzy matching - hello~2 allows up to 2 edits
36//! let re = FuzzyRegex::builder("hello~2")
37//!     .similarity(0.7)
38//!     .build()
39//!     .unwrap();
40//!
41//! assert!(re.is_match("hello"));  // Exact match
42//! assert!(re.is_match("helo"));   // 1 deletion
43//! assert!(re.is_match("helllo")); // 1 insertion
44//! ```
45//!
46//! ## Pattern Syntax
47//!
48//! ### Fuzziness Markers
49//!
50//! - `hello~2` - Allow up to 2 edits in "hello"
51//! - `hello~0` - Exact match only (no edits)
52//! - `hello~{i=1,d=2,s=0}` - Detailed limits: 1 insertion, 2 deletions, 0 substitutions
53//! - `(hello world)~2` - Fuzziness on entire group
54//!
55//! ### Standard Regex Constructs
56//!
57//! - `[a-z]`, `\d`, `\w`, `\s` - Character classes
58//! - `*`, `+`, `?`, `{n,m}` - Quantifiers
59//! - `(group)`, `(?:non-capture)` - Groups
60//! - `(?<name>...)` - Named capture groups
61//! - `a|b` - Alternation
62//! - `^`, `$` - Anchors
63//! - `(?=...)`, `(?!...)` - Lookahead
64//!
65//! ## Examples
66//!
67//! ### Fuzzy Search
68//!
69//! ```rust
70//! use fuzzy_regex::FuzzyRegexBuilder;
71//!
72//! let re = FuzzyRegexBuilder::new(r"color~2|colour~2")
73//!     .similarity(0.8)
74//!     .build()
75//!     .unwrap();
76//!
77//! // Matches various spellings
78//! assert!(re.is_match("color"));
79//! assert!(re.is_match("colour"));
80//! assert!(re.is_match("colur")); // With typo
81//! ```
82//!
83//! ### Capture Groups with Fuzzy Matching
84//!
85//! ```rust
86//! use fuzzy_regex::FuzzyRegex;
87//!
88//! let re = FuzzyRegex::new(r"(?<word>\w+)").unwrap();
89//! let caps = re.captures("hello").unwrap();
90//! assert_eq!(caps.name("word").unwrap().as_str(), "hello");
91//! ```
92//!
93//! ### Replace with Fuzzy Matching
94//!
95//! ```rust
96//! use fuzzy_regex::FuzzyRegexBuilder;
97//!
98//! // Match "recieve" (common misspelling) and replace with correct spelling
99//! // Note: fuzzy matching allows edits including insertions/deletions at boundaries,
100//! // so we use exact match here (~0) to replace just the misspelled word
101//! let re = FuzzyRegexBuilder::new(r"recieve~0")
102//!     .build()
103//!     .unwrap();
104//!
105//! let result = re.replace("Did you recieve the package?", "receive");
106//! assert_eq!(result, "Did you receive the package?");
107//! ```
108
109// Re-export mimalloc for consumers who want to use it as global allocator
110#[cfg(feature = "mimalloc")]
111pub use mimalloc::MiMalloc;
112
113pub mod api;
114pub mod compat;
115/// Compiler for converting parsed regex AST into executable IR.
116pub mod compiler;
117/// Core matching engines including NFA simulation and Bitap algorithm.
118pub mod engine;
119pub mod error;
120/// Intermediate representation for compiled regex patterns.
121pub mod ir;
122/// Regex pattern parser and lexer.
123pub mod parser;
124pub mod types;
125
126// Re-export main types at crate root
127pub use api::{
128    ByteMatches, CaptureMatches, Captures, FeedMatches, FuzzyRegex, FuzzyRegexBuilder, Match,
129    MatchFlags, Matches, ReaderMatches, RegexConfig, Split, StreamingMatch, StreamingMatcher,
130};
131pub use engine::EditCounts;
132pub use error::{Error, Result};
133pub use types::{FuzzyLimits, FuzzyPenalties};