Struct onig::Regex
[−]
[src]
pub struct Regex { // some fields omitted }
This struct is a wrapper around an Oniguruma regular expression pointer. This represents a compiled regex which can be used in search and match operations.
Methods
impl Regex
[src]
fn new(pattern: &str) -> Result<Regex, Error>
Simple regular expression constructor. Compiles a new regular expression with the default options using the ruby syntax. Once compiled, it can be used repeatedly to search in a string. If an invalid expression is given, then an error is returned.
Arguments
pattern
- The regex pattern to compile
Examples
use onig::Regex; let r = Regex::new(r#"hello (\w+)"#); assert!(r.is_ok());
fn with_options(pattern: &str, option: RegexOptions, syntax: &Syntax) -> Result<Regex, Error>
Create a new Regex
Attempts to compile a pattern into a new Regex
instance.
Once compiled, it can be used repeatedly to search in a string. If an
invalid expression is given, then an error is returned.
See onig_sys::onig_new
for more information.
Arguments
pattern
- The regex pattern to compile.options
- The regex compilation options.syntax
- The syntax which the regex is written in.
Examples
use onig::{Regex, Syntax, REGEX_OPTION_NONE}; let r = Regex::with_options("hello.*world", REGEX_OPTION_NONE, Syntax::default()); assert!(r.is_ok());
fn match_with_options(&self, str: &str, options: SearchOptions, region: Option<&mut Region>) -> Option<usize>
Match string
Match the regex against a string. This method will start at
the beginning of the string and try and match the regex. If
the regex matches then the return value is the number of
characers which matched. If the regex doesn't match the return
is None
.
Arguments
str
- The string slice to match against.options
- The regex match options.region
- The region for return group match range info
Returns
Some(len)
if the regex matched, with len
being the number
of bytes matched. None
if the regex doesn't match.
Examples
use onig::{Regex, SEARCH_OPTION_NONE}; let r = Regex::new(".*").unwrap(); let res = r.match_with_options("hello", SEARCH_OPTION_NONE, None); assert!(res.is_some()); // it matches assert!(res.unwrap() == 5); // 5 characters matched
fn search_with_options(&self, str: &str, options: SearchOptions, region: Option<&mut Region>) -> Option<usize>
Search pattern in string
Search for matches the regex in a string. This method will return the index of the first match of the regex within the string, if there is one.
Arguments
str
- The string to search in.options
- The options for the search.region
- The region for return group match range info
Returns
Some(pos)
if the regex matches, where pos
is the
byte-position of the start of the match. None
if the regex
doesn't match anywhere in str
.
Examples
use onig::{Regex, SEARCH_OPTION_NONE}; let r = Regex::new("l{1,2}").unwrap(); let res = r.search_with_options("hello", SEARCH_OPTION_NONE, None); assert!(res.is_some()); // it matches assert!(res.unwrap() == 2); // match starts at character 3
fn is_match(&self, text: &str) -> bool
Returns true if and only if the regex matches the string given.
fn find(&self, text: &str) -> Option<(usize, usize)>
Returns the start and end byte range of the leftmost-first match in
text
. If no match exists, then None
is returned.
Note that this should only be used if you want to discover the position
of the match. Testing the existence of a match is faster if you use
is_match
.
fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>>
Returns the capture groups corresponding to the leftmost-first match
in text. Capture group 0
always corresponds to the entire match.
If no match is found, then None
is returned.