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§

pub use tre_regex_sys as tre;

Structs§

BindingErrorCode
Custom error type for errors in the binding itself.
RegApproxMatch
This struct is returned by regaexec and friends.
RegApproxParams
Regex params passed to approximate matching functions such as regaexec
RegcompFlags
Flags to pass to regcomp.
Regex
The base regex object.
RegexError
Error type returned in results
RegexecFlags
Flags to pass to regexec.

Enums§

ErrorKind
Type of error: Binding (see BindingErrorCode), or Tre

Functions§

regaexec
Performs an approximate regex search on the passed string, returning nmatches results.
regaexec_bytes
Performs an approximate regex search on the passed bytes, returning nmatches results.
regawexec
Performs an approximate regex search on the passed wide string, returning nmatches results.
regcomp
Compiles a regex.
regcomp_bytes
Compiles a regex that is in the form of bytes.
regerror
Given a Regex struct and ErrorInt code, build a RegexError.
regexec
Performs a regex search on the passed string, returning nmatches results.
regexec_bytes
Performs a regex search on the passed bytes, returning nmatches results.
regwcomp
Compiles a regex that is in the form of a WideStr.
regwexec
Performs a regex search on the passed wide string, returning nmatches results.

Type Aliases§

ErrorInt
RegApproxMatchBytes
RegApproxMatchStr
RegApproxMatchWideStr
RegFlags
RegMatchBytes
RegMatchStr
RegMatchWideStr
Result