Crate reggy

Source
Expand description

A friendly regular expression dialect for text analytics. Typical regex features are removed/adjusted to make natural language queries easier. Unicode-aware and able to search a stream with several patterns at once.

§API Usage

Use the high-level Pattern struct for simple search.

use reggy::Pattern;

let mut p = Pattern::new("dogs?")?;
assert_eq!(
    p.findall("cat dog dogs cats"),
    vec![(4, 7), (8, 12)]
);

Use the Ast struct to transpile to normal regex syntax.

use reggy::Ast;

let ast = Ast::parse(r"dog(gy)?|dawg|(!CAT|KITTY CAT)")?;
assert_eq!(
    ast.to_regex(),
    r"\b(?mi:dog(?:gy)?|dawg|(?-i:CAT|KITTY\s+CAT))\b"
);

Use the Search struct to search a stream with several patterns at once.

use reggy::{Search, Match};

let mut search = Search::compile(&[
    r"$#?#?#.##",
    r"(John|Jane) Doe"
])?;

// Call Search::next to begin searching.
// It will yield any matches deemed definitely-complete immediately.
let jane_match = Match::new(1, (0, 8));
assert_eq!(
    search.next("Jane Doe paid John"),
    vec![jane_match]
);

// Call Search::next again to continue with the same search state.
// Note that "John Doe" matched across the chunk boundary.
// Spans are relative to the start of the stream.
let john_match = Match::new(1, (14, 22));
let money_match_1 = Match::new(0, (23, 29));
let money_match_2 = Match::new(0, (41, 48));
assert_eq!(
    search.next(" Doe $45.66 instead of $499.00"),
    vec![john_match, money_match_1, money_match_2]
);
 
// Call `Search::finish` to collect any not-definitely-complete matches once the stream is closed.
assert_eq!(search.finish(), vec![]);

Structs§

Match
A match object returned from a Search
Pattern
A high-level interface for matching a single reggy pattern
Search
A compiled searcher for multiple patterns against a stream of text
StreamSearch
A stream search for the provided BufReader

Enums§

Ast
A reggy pattern represented as an AST
Error
An error raised while parsing a reggy pattern
SearchStreamError
An error raised while searching a stream