xur_

> module

std::regex

regular expressions

use std::regex; feature regex in the browser

std::regex provides regular expressions using the Rust regex crate's syntax: Perl-like, but without look-around or backreferences, and matching in linear time. Matching works on Unicode text.

> functions

# new(pattern: str) -> Result<Regex, Error>

Compiles pattern. Returns Err(regex::Error::Invalid(message)) if it isn't a valid regex.

> types

┌ enum Error

A regex error. The pattern failed to compile; the message says why. Match it as `Err(regex::Error::Invalid(msg))`.

Error::Invalid(str)
┌ type Regex
# regex.captures(text: str) -> Option<list<Option<str>>>

The groups of the first match: item 0 is the whole match, then one item per group, with None for a group that didn't take part. Returns None if there is no match.

# regex.find(text: str) -> Option<str>

The first match, or None.

# regex.find_all(text: str) -> list<str>

All non-overlapping matches, left to right.

# regex.is_match(text: str) -> bool

Whether the pattern matches anywhere in text.

# regex.replace(text: str, replacement: str) -> str

Replaces the first match, or every match. In replacement, $1 or ${name} inserts a group and $$ a literal $.

# regex.replace_all(text: str, replacement: str) -> str

Replaces the first match, or every match. In replacement, $1 or ${name} inserts a group and $$ a literal $.

# regex.split(text: str) -> list<str>

The pieces of text between matches.

> errors

regex::Error has one variant, Invalid(str), with the compiler's message.

> examples

use std::regex;
use std::io;

fn main() -> Result<()> {
    let re = regex::new("\\d+")?;
    io::println("{}", re.find_all("a1 b22 c333"));
    // => ["1", "22", "333"]

    let email = regex::new("(\\w+)@(\\w+\\.\\w+)")?;
    match email.captures("user@example.com") {
        Some(caps) => io::println("{} at {}", caps[1].unwrap(), caps[2].unwrap()),
        None => io::println("no match"),
    }
    Ok(())
}