Search This Blog

Regular Expression Capture Groups

  • Capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap[1]);
    }
    
    
  • Named capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(?<word>\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap["word"]);
    }
    
    

No comments:

Post a Comment