auto-regex

Crates.ioauto-regex
lib.rsauto-regex
version0.1.3
sourcesrc
created_at2024-08-30 14:35:04.767731
updated_at2024-08-31 16:33:27.844205
descriptionAutomagically finds a regex that best matches an example and a sample list.
homepage
repositoryhttps://github.com/Inspirateur/auto-regex
max_upload_size
id1357749
size13,246
Téo Orthlieb (Inspirateur)

documentation

README

auto-regex

Rust crate to find a regex rules that best matches a list of string

Example

use auto_regex::infer_regex;

fn main() {
    // This has very little interest in static code 
    // and is best used with user interaction
    let samples = vec![
        "john.doe@gmail.com".to_string(),
        "alice.smith@gmail.com".to_string(),
        "bob.harris@gmail.com".to_string(),
        // bad samples will be ignored
        "badsample".to_string(),
    ];
    let example = "firstname.lastname@gmail.com".to_string();
    let regex = infer_regex(example, samples).unwrap();
    // Prints '(?i)^(.+)\.(.+)@gmail\.com$'
    // a regex that will extract first and last name from a gmail
    println!("{}", regex.as_str());
}

Use cases

Used in Inspirateur/SimpleRenamer, a smart file renamer.

It can generally be useful in applications to give Regex power to users who don't know Regex or don't want to bother with it.

Commit count: 0

cargo fmt