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§
- Binding
Error Code - Custom error type for errors in the binding itself.
- RegApprox
Match - This struct is returned by
regaexec
and friends. - RegApprox
Params - Regex params passed to approximate matching functions such as
regaexec
- Regcomp
Flags - Flags to pass to
regcomp
. - Regex
- The base regex object.
- Regex
Error - Error type returned in results
- Regexec
Flags - Flags to pass to
regexec
.
Enums§
- Error
Kind - Type of error:
Binding
(seeBindingErrorCode
), orTre
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 andErrorInt
code, build aRegexError
. - 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.