Patterns as sets of strings.

The regex crate tells you whether a string matches a pattern. RegexSolver treats patterns as the sets of strings they match, so you can intersect, subtract, compare, complement and enumerate them, and get the result back as a regex.

Try it

The strings both patterns match, as one pattern.

A learned model proposes a simpler pattern, checked against the automaton. Downloads 21 MB on first use.

RESULT
› THE SAME CALL IN RUST
use regexsolver::Term;
use regexsolver::neural_synthesis::{Device, NeuralSynthesizer};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let a: Term = "/api/v[12]/.*".parse()?;
    let b: Term = ".*/users/[0-9]+".parse()?;

    let result = a.intersection([&b])?;

    let synthesizer = NeuralSynthesizer::from_hub(
        "alexvbrdn/kleene2-5m-b16-t128",
        Device::best_available(),
    )?;
    println!("{}", result.to_pattern_with(&synthesizer)?);
    Ok(())
}

Example

use regexsolver::{Term, fast_automaton::PathOrder};
use regexsolver::neural_synthesis::{Device, NeuralSynthesizer};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let a: Term = "(ab|xy){2}".parse()?;
    let b: Term = ".*xy".parse()?;

    // Which strings match BOTH patterns? Get the answer as a pattern:
    let both = a.intersection([&b])?;
    println!("{}", both.to_pattern()?);
    // (ab|xy)xy

    // Test a concrete string against the result (matching is anchored):
    println!("{}", both.is_match("abxy")?);
    // true

    // ...and sample them:
    println!("{:?}", both.generate_strings(2, 0, PathOrder::Sweep)?);
    // ["xyxy", "abxy"]

    // Words ending in "ing", except those ending in "ring":
    let ing: Term = "[a-z]*ing".parse()?;
    let ring: Term = "[a-z]*ring".parse()?;
    let words = ing.difference(&ring)?;

    // State elimination
    println!("{}", words.to_pattern()?);
    // [a-qs-z]*(i|r((i?|ing?|([a-hj-qs-z]|i[a-mo-qs-z]|in([a-fh-qs-z]|g[a-qs-z]))[a-qs-z]*)r)*(i{2}|ing?i|([a-hj-qs-z]|i[a-mo-qs-z]|in([a-fh-qs-z]|g[a-qs-z]))[a-qs-z]*i))ng

    // Neural synthesis: a learned model proposes a simpler pattern, checked
    // against the automaton (feature `neural-synthesis-hub`).
    let synthesizer = NeuralSynthesizer::from_hub(
        NeuralSynthesizer::DEFAULT_HUB_MODEL,
        Device::best_available(),
    )?;
    println!("{}", words.to_pattern_with(&synthesizer)?);
    // ([a-z]*[a-qs-z])?ing
    Ok(())
}

Use cases

  • Safe migrations: old.subset(&new)? tells you whether a new validation pattern accepts everything the old one did.
  • Equivalence proofs: a.equivalent(&b)? shows that two patterns match the same strings.
  • Rule analysis: intersection and difference find shadowed or overlapping routes, firewall rules and validators.
  • Test data: generate_strings produces strings that match a pattern, spread over the cases it allows, reproducible and pageable.
  • Input bounds: length() gives the shortest and longest string a pattern matches, to size a database column.
  • Readable regexes: to_pattern converts every result back to a pattern, and neural synthesis can make it simpler: try both in the demo.

Semantics

  • Everything is anchored: abc matches the string “abc”, not “xabc” or “abcx”. A pattern describes whole strings.
  • . matches any character, including line feed.
  • Constructs a regular language can’t express, such as backreferences, lookaround, inline flags and word boundaries, return an error instead of changing what the pattern means.
  • Automaton operations can blow up on adversarial patterns. To run untrusted input, set limits on run time and number of states with an ExecutionProfile.

The hosted REST API and its SDKs have been retired: the crate is the way to use RegexSolver.