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]

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.

Returns an iterator for each successive non-overlapping match in text, returning the start and end byte indices with respect to text.

Example

Find the start and end location of every word with exactly 13 characters:

let text = "Retroactively relinquishing remunerations is reprehensible.";
for pos in Regex::new(r"\b\w{13}\b").unwrap().find_iter(text) {
    println!("{:?}", pos);
}
// Output:
// (0, 13)
// (14, 27)
// (28, 41)
// (45, 58)

Returns an iterator over all the non-overlapping capture groups matched in text. This is operationally the same as find_iter (except it yields information about submatches).

Example

We can use this to find all movie titles and their release years in some text, where the movie is formatted like "'Title' (xxxx)":

let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)")
               .unwrap();
let text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
for caps in re.captures_iter(text) {
    println!("Movie: {:?}, Released: {:?}", caps.at(1), caps.at(2));
}
// Output:
// Movie: Citizen Kane, Released: 1941
// Movie: The Wizard of Oz, Released: 1939
// Movie: M, Released: 1931

Returns an iterator of substrings of text delimited by a match of the regular expression. Namely, each element of the iterator corresponds to text that isn't matched by the regular expression.

This method will not copy the text given.

Example

To split a string delimited by arbitrary amounts of spaces or tabs:

let re = Regex::new(r"[ \t]+").unwrap();
let fields: Vec<&str> = re.split("a b \t  c\td    e").collect();
assert_eq!(fields, vec!("a", "b", "c", "d", "e"));

Returns an iterator of at most limit substrings of text delimited by a match of the regular expression. (A limit of 0 will return no substrings.) Namely, each element of the iterator corresponds to text that isn't matched by the regular expression. The remainder of the string that is not split will be the last element in the iterator.

This method will not copy the text given.

Example

Get the first two words in some text:

let re = Regex::new(r"\W+").unwrap();
let fields: Vec<&str> = re.splitn("Hey! How are you?", 3).collect();
assert_eq!(fields, vec!("Hey", "How", "are you?"));

Scan the given slice, capturing into the given region and executing a callback for each match.

impl Regex
[src]

Replaces the leftmost-first match with the replacement provided. The replacement can be a regular string or a function that takes the matches Captures and returns the replaced string.

If no match is found, then a copy of the string is returned unchanged.

Examples

Note that this function is polymorphic with respect to the replacement. In typical usage, this can just be a normal string:

let re = Regex::new("[^01]+").unwrap();
assert_eq!(re.replace("1078910", ""), "1010");

But anything satisfying the Replacer trait will work. For example, a closure of type |&Captures| -> String provides direct access to the captures corresponding to a match. This allows one to access submatches easily:

let re = Regex::new(r"([^,\s]+),\s+(\S+)").unwrap();
let result = re.replace("Springsteen, Bruce", |caps: &Captures| {
    format!("{} {}", caps.at(2).unwrap_or(""), caps.at(1).unwrap_or(""))
});
assert_eq!(result, "Bruce Springsteen");

Replaces all non-overlapping matches in text with the replacement provided. This is the same as calling replacen with limit set to 0.

See the documentation for replace for details on how to access submatches in the replacement string.

Replaces at most limit non-overlapping matches in text with the replacement provided. If limit is 0, then all non-overlapping matches are replaced.

See the documentation for replace for details on how to access submatches in the replacement string.

impl Regex
[src]

Returns the number of named groups into regex.

Returns the iterator over named groups as a tuple with the group name and group indexes.

impl Regex
[src]

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());

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());

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.
  • at - The byte index in the passed slice to start matching
  • 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", 0, SEARCH_OPTION_NONE, None);
assert!(res.is_some()); // it matches
assert!(res.unwrap() == 5); // 5 characters matched

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. If from is less than to, then search is performed in forward order, otherwice – in backward order.

Arguments

  • str - The string to search in.
  • from - The byte index in the passed slice to start search
  • to - The byte index in the passed slice to finish search
  • 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", 0, 5, SEARCH_OPTION_NONE, None);
assert!(res.is_some()); // it matches
assert!(res.unwrap() == 2); // match starts at character 3

Returns true if and only if the regex matches the string given.

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.

Trait Implementations

impl Debug for Regex
[src]

Formats the value using the given formatter.

impl Eq for Regex
[src]

impl PartialEq for Regex
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Drop for Regex
[src]

A method called when the value goes out of scope. Read more