Crate tre_regex

source ·
Expand description

These are safe bindings to the tre_regex_sys module.

These bindings are designed to provide an idiomatic Rust-like API to the TRE library as much as possible. Most of the TRE API is suported, except reguexec from TRE; that is tricky to implement, although should be fairly simple to use yourself.

This library uses Rust std::borrow::Cow strings to enable zero-copy of regex matches.

§Examples

Two API’s are presented: the function API, and the object API. Whichever one you choose to use is up to you, although the function API is implemented as a thin wrapper around the object API.

§Object API

use tre_regex::{RegcompFlags, RegexecFlags, Regex};

let regcomp_flags = RegcompFlags::new().add(RegcompFlags::EXTENDED);
let regexec_flags = RegexecFlags::new().add(RegexecFlags::NONE);

let compiled_reg = Regex::new("^([[:alnum:]]+)[[:space:]]*([[:alnum:]]+)$", regcomp_flags)?;
let matches = compiled_reg.regexec("hello world", 2, regexec_flags)?;

for (i, matched) in matches.into_iter().enumerate() {
    match matched {
        Some(res) => {
            match res {
                Ok(substr) => println!("Match {i}: '{}'", substr),
                Err(e) => println!("Match {i}: <Error: {e}>"),
            }
        },
        None => println!("Match {i}: <None>"),
    }
}

§Function API

use tre_regex::{RegcompFlags, RegexecFlags, regcomp, regexec};

let regcomp_flags = RegcompFlags::new().add(RegcompFlags::EXTENDED);
let regexec_flags = RegexecFlags::new().add(RegexecFlags::NONE);

let compiled_reg = regcomp("^([[:alnum:]]+)[[:space:]]*([[:alnum:]]+)$", regcomp_flags)?;
let matches = regexec(&compiled_reg, "hello world", 2, regexec_flags)?;

for (i, matched) in matches.into_iter().enumerate() {
    match matched {
        Some(res) => {
            match res {
                Ok(substr) => println!("Match {i}: '{}'", substr),
                Err(e) => println!("Match {i}: <Error: {e}>"),
            }
        },
        None => println!("Match {i}: <None>"),
    }
}

Re-exports§

Structs§

Enums§

Functions§

  • Performs an approximate regex search on the passed string, returning nmatches results.
  • Performs an approximate regex search on the passed bytes, returning nmatches results.
  • Performs an approximate regex search on the passed wide string, returning nmatches results.
  • Compiles a regex.
  • Compiles a regex that is in the form of bytes.
  • Given a Regex struct and ErrorInt code, build a RegexError.
  • Performs a regex search on the passed string, returning nmatches results.
  • Performs a regex search on the passed bytes, returning nmatches results.
  • Compiles a regex that is in the form of a WideStr.
  • Performs a regex search on the passed wide string, returning nmatches results.

Type Aliases§