Skip to main content

demo/
demo.rs

1//! A small demonstration of the eregex API.
2
3use eregex::{Regex, flags};
4
5fn main() {
6    let re = Regex::new(r"(?P<word>\w+)\s+(?P<num>\d+)").unwrap();
7    let m = re.find("hello 42 world").unwrap();
8    println!("match: {:?}", m.as_str());
9    println!("word = {:?}", m.name("word"));
10    println!("num  = {:?}", m.name("num"));
11
12    // Repeated captures — a signature mrab-regex feature.
13    let re = Regex::new(r"(\w)+").unwrap();
14    let m = re.find("abc").unwrap();
15    println!("captures of group 1: {:?}", m.captures(1));
16
17    // Lookbehind (variable length) + case-insensitive.
18    let re = Regex::new(r"(?i)(?<=foo)bar").unwrap();
19    println!("lookbehind: {:?}", re.find("FOObar"));
20
21    // Atomic group prevents catastrophic backtracking.
22    let re = Regex::new(r"a(?>b*)b").unwrap();
23    println!("atomic find: {:?}", re.find("abbbbc"));
24
25    // Replace with named groups.
26    let re = Regex::new(r"(?P<first>\w+),(?P<second>\w+)").unwrap();
27    println!(
28        "replace: {}",
29        re.replace_all("a,b x,y", "${second} ${first}")
30    );
31
32    // Flags constant.
33    let re = Regex::new_with_flags(r"hello", flags::IGNORECASE | flags::MULTILINE).unwrap();
34    println!("flags: {:x}", re.flags().bits());
35}