File: names.rs

package info (click to toggle)
rust-onig 6.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: makefile: 4
file content (40 lines) | stat: -rw-r--r-- 924 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
extern crate onig;

use onig::*;

fn main() {
    let pattern = "(?<foo>a*)(?<bar>b*)(?<foo>c*)";
    let string = "aaabbbbcc";

    let r = Regex::new(pattern).unwrap();

    println!("has {} group names:", r.capture_names_len());

    r.foreach_name(|name, indices| {
        println!("- {}: {:?}", name, indices);
        true
    });

    let mut region = Region::new();

    if let Some(position) = r.search_with_options(
        string,
        0,
        string.len(),
        SearchOptions::SEARCH_OPTION_NONE,
        Some(&mut region),
    ) {
        println!("match at {} in {:?}", position, string);

        r.foreach_name(|name, groups| {
            for group in groups {
                let pos = region.pos(*group as usize).unwrap();
                println!("- {} ({}): {} - {}", name, group, pos.0, pos.1);
            }

            true
        });
    } else {
        println!("search fail")
    }
}