A compiled regular expression.
A Regex is safe to use from multiple contexts, as it contains no mutable state.
Once compiled, it can be used to match against text repeatedly.
Examples
use std::regex::Regex;
let re = Regex::compile("foo.*bar").unwrap();
defer re.free();
assert!(re.is_match("foobar"));
assert!(re.is_match("foo123bar"));
assert!(!re.is_match("foobaz"));
Run this example
Methods
impl Regex { ... }
-
Compile a regex pattern
Example
use std::regex::Regex; let re = Regex::compile("foo.*bar").unwrap(); defer re.free(); assert!(re.is_match("foobar"));Run this example -
Check if the entire text matches the pattern
Example
use std::regex::Regex; let re = Regex::compile("^[0-9]+$").unwrap(); defer re.free(); assert!(re.is_match("123")); assert!(!re.is_match("abc"));Run this example -
Find the first match in the text Returns (start_pos, end_pos) if found
Example
use std::regex::Regex; let re = Regex::compile("\\d+").unwrap(); defer re.free(); let result = re.find("abc123def"); assert_eq!(result, Option::some((3usize, 6usize)));Run this example -
Check if text matches from the beginning
Example
use std::regex::Regex; let re = Regex::compile("foo").unwrap(); defer re.free(); assert!(re.match_from_start("foobar")); assert!(!re.match_from_start("barfoo"));Run this example -
Find first match and return capture groups
Example
use std::regex::Regex; let re = Regex::compile("(\\d+)-(\\d+)").unwrap(); defer re.free(); let caps = re.captures("foo 123-456 bar").unwrap(); defer caps.free(); assert_eq!(caps.get(0).unwrap(), "123-456"); assert_eq!(caps.get(1).unwrap(), "123"); assert_eq!(caps.get(2).unwrap(), "456");Run this example -
fn free(self: &mut Regex)
Frees the memory backing the object.