Crates.io | auto-regex |
lib.rs | auto-regex |
version | 0.1.3 |
source | src |
created_at | 2024-08-30 14:35:04.767731 |
updated_at | 2024-08-31 16:33:27.844205 |
description | Automagically finds a regex that best matches an example and a sample list. |
homepage | |
repository | https://github.com/Inspirateur/auto-regex |
max_upload_size | |
id | 1357749 |
size | 13,246 |
Rust crate to find a regex rules that best matches a list of string
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());
}
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.