Using regular expressions

Rust:
Using regular expressions

How to:

Rust’s regex library is a go-to for working with regular expressions. To use it, you’ll first need to add it to your Cargo.toml:

[dependencies]
regex = "1"

Then, you can start implementing regex functionalities in your Rust code. Here’s how to perform some common operations:

Matching a Pattern in a String

use regex::Regex;

fn main() {
    let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
    let date = "2023-04-15";

    println!("Does the text match the date pattern? {}", re.is_match(date));
    // Output: Does the text match the date pattern? true
}

Finding and Accessing Matches

use regex::Regex;

fn main() {
    let text = "Rust 2023, C++ 2022, Python 2021";
    let re = Regex::new(r"\b(\w+)\s(\d{4})").unwrap();

    for cap in re.captures_iter(text) {
        println!("Language: {}, Year: {}", &cap[1], &cap[2]);
    }
    // Output:
    // Language: Rust, Year: 2023
    // Language: C++, Year: 2022
    // Language: Python, Year: 2021
}

Replacing Text

use regex::Regex;

fn main() {
    let re = Regex::new(r"\b(\w+)\s(\d{4})").unwrap();
    let text = "Rust 2023, C++ 2022, Python 2021";
    let replaced = re.replace_all(text, "$1 was updated in $2");

    println!("Updated text: {}", replaced);
    // Output: Updated text: Rust was updated in 2023, C++ was updated in 2022, Python was updated in 2021
}

Splitting Text Using a Regex

use regex::Regex;

fn main() {
    let re = Regex::new(r"\W+").unwrap(); // split at any non-word character
    let text = "Rust-C++-Python-Go";

    let fields: Vec<&str> = re.split(text).collect();

    for field in fields {
        println!("Language: {}", field);
    }
    // Output:
    // Language: Rust
    // Language: C++
    // Language: Python
    // Language: Go
}

These examples provide a basic guide to getting started with regular expressions in Rust. As your needs become more sophisticated, the regex crate offers a wealth of functionality for complex pattern matching and text manipulation tasks.