Struct ::regex::Regex

A UTF-8 string regular expression

Methods

fn new(pattern: String) -> Result

Create a new regex from a string

fn is_match(self, text: String) -> bool

Check if the regex matches the string

fn find(self, text: String) -> Option

Find the first match in the string

fn replace(self, text: String, replace: String) -> String

Replace the leftmost match in the string.

Capture groups can be referred to via $1, $2, etc. ($0 is the full match). Named capture groups are supported via $name. You can also use ${name} or ${1} etc., which is often needed to disambiguate when a capture group number is followed by literal text.

fn replace_all(self, text: String, replace: String) -> String

Replace all matches in the string

Capture groups can be referred to via $1, $2, etc. ($0 is the full match). Named capture groups are supported via $name.

fn captures(self, text: String) -> Option

Capture groups

  • If no match is found returns None.
  • Otherwise Some(vector of optional strings) where:
    • The first group (index 0) is the full match as Some(value).
    • The rest are the capture groups. If they didn't match they are None. Otherwise, they are Some(value).