Pure Programmer
Blue Matrix


Cluster Map

Regular Expressions

L1

This page is under construction. Please come back later.

The regex construct is not a built-in part of Rust. You will need to install the crate regex to use it.
$ cargo new RegEx1 $ cd RegEx1 $ cargo add regex
RegEx1.rs
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use regex::Regex;

fn main() {
	let s:&'static str = "Four score and seven years ago...";
	println!("Match 1: {}", utils::regex_search(Regex::new(r"s.*e").unwrap(), s));
	println!("Match 2: {}", utils::regex_search(Regex::new(r"\bs.*e\b").unwrap(), s));
	println!("Match 3: {}", utils::regex_search(Regex::new(r"s...e").unwrap(), s));
	println!("Match 4: {}", utils::regex_search(Regex::new(r"b.d").unwrap(), s));

	let mut subgroups:Vec<String> = utils::regex_find_first(Regex::new(r"(\w+)\s*(\w+)").unwrap(), s);
	println!("Find First (with subgroups): {}", list_to_string!(subgroups));
	subgroups = utils::regex_find_first(Regex::new(r"bad match").unwrap(), s);
	println!("Find First (bad match): {}", list_to_string!(subgroups));

	let mut matches:Vec<String> = utils::regex_find_all(Regex::new(r"\w+").unwrap(), s);
	println!("Find All: (matches only){}", list_to_string!(matches));
	matches = utils::regex_find_all(Regex::new(r"bad match").unwrap(), s);
	println!("Find All (bad match): {}", list_to_string!(matches));
}

Output
$ rustc RegEx1.rs error: expected one of `.`, `?`, `{`, or an operator, found `<eof>` --> RegEx1.rs:6:62 | 6 | println!("Match 1: {}", match(s,Regex::new(r"s.*e").unwrap())); | ----- ^ expected one of `.`, `?`, `{`, or an operator | | | while parsing this `match` expression error: expected one of `.`, `?`, `{`, or an operator, found `<eof>` --> RegEx1.rs:7:68 | 7 | println!("Match 2: {}", match(s,Regex::new(r"\\bs.*e\\b").unwrap())); | ----- ^ expected one of `.`, `?`, `{`, or an operator | | | while parsing this `match` expression error: expected one of `.`, `?`, `{`, or an operator, found `<eof>` --> RegEx1.rs:8:63 | 8 | println!("Match 3: {}", match(s,Regex::new(r"s...e").unwrap())); | ----- ^ expected one of `.`, `?`, `{`, or an operator | | | while parsing this `match` expression error: expected one of `.`, `?`, `{`, or an operator, found `<eof>` --> RegEx1.rs:9:61 | 9 | println!("Match 4: {}", match(s,Regex::new(r"b.d").unwrap())); | ----- ^ expected one of `.`, `?`, `{`, or an operator | | | while parsing this `match` expression error[E0432]: unresolved import `regex` --> RegEx1.rs:2:5 | 2 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `findFirst` in this scope --> RegEx1.rs:11:34 | 11 | let mut subgroups:Vec<String> = findFirst(s,Regex::new(r"(\\w+)\\s*(\\w+)").unwrap()); | ^^^^^^^^^ not found in this scope error[E0425]: cannot find function `findFirst` in this scope --> RegEx1.rs:13:14 | 13 | subgroups = findFirst(s,Regex::new(r"bad match").unwrap()); | ^^^^^^^^^ not found in this scope error[E0425]: cannot find function `findAll` in this scope --> RegEx1.rs:16:32 | 16 | let mut matches:Vec<String> = findAll(s,Regex::new(r"\\w+").unwrap()); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `findAll` in this scope --> RegEx1.rs:18:12 | 18 | matches = findAll(s,Regex::new(r"bad match").unwrap()); | ^^^^^^^ not found in this scope error: aborting due to 9 previous errors Some errors have detailed explanations: E0425, E0432. For more information about an error, try `rustc --explain E0425`.
RegEx2.rs
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use regex::Regex;

fn main() {
	let s:&'static str = "Four score and seven years ago...";
	println!("Replace First 1: {}", utils::regex_replace_first(Regex::new(r"\.\.\.").unwrap(), s, "!!!"));
	println!("Replace First 2: {}", utils::regex_replace_first(Regex::new(r"\b...\b").unwrap(), s, "???"));
	println!("Replace First 3: {}", utils::regex_replace_first(Regex::new(r"b.d").unwrap(), s, "???"));
	println!("Replace First 4: {}", utils::regex_replace_first(Regex::new(r"(\w+) (\w+)").unwrap(), s, "$2 $1"));

	println!("Replace All 1: {}", utils::regex_replace_all(Regex::new(r"\b...\b").unwrap(), s, "???"));
	println!("Replace All 2: {}", utils::regex_replace_all(Regex::new(r"(\w+) (\w+)").unwrap(), s, "$2 $1"));
}

Output
$ rustc RegEx2.rs error[E0432]: unresolved import `regex` --> RegEx2.rs:2:5 | 2 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `replaceFirst` in this scope --> RegEx2.rs:6:34 | 6 | println!("Replace First 1: {}", replaceFirst(s,Regex::new(r"\\.\\.\\.").unwrap(),"!!!")); | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceFirst` in this scope --> RegEx2.rs:7:34 | 7 | println!("Replace First 2: {}", replaceFirst(s,Regex::new(r"\\b...\\b").unwrap(),"???")); | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceFirst` in this scope --> RegEx2.rs:8:34 | 8 | println!("Replace First 3: {}", replaceFirst(s,Regex::new(r"b.d").unwrap(),"???")); | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceFirst` in this scope --> RegEx2.rs:9:34 | 9 | println!("Replace First 4: {}", replaceFirst(s,Regex::new(r"(\\w+) (\\w+)").unwrap(),"$2 $1")); | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceAll` in this scope --> RegEx2.rs:11:32 | 11 | println!("Replace All 1: {}", replaceAll(s,Regex::new(r"\\b...\\b").unwrap(),"???")); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceAll` in this scope --> RegEx2.rs:12:32 | 12 | println!("Replace All 2: {}", replaceAll(s,Regex::new(r"(\\w+) (\\w+)").unwrap(),"$2 $1")); | ^^^^^^^^^^ not found in this scope error: aborting due to 7 previous errors Some errors have detailed explanations: E0425, E0432. For more information about an error, try `rustc --explain E0425`.
RegEx3.rs
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use regex::Regex;

fn main() {
	let str:&'static str = "Four score and seven years ago...";
	println!("Split 1: {}", list_to_string!(utils::regex_split(Regex::new(r" ").unwrap(), str)));
	println!("Split 2: {}", list_to_string!(utils::regex_split(Regex::new(r"[eo]").unwrap(), str)));
	println!("Split 3: {}", list_to_string!(utils::regex_split(Regex::new(r"\s").unwrap(), str)));
	println!("Split 4: {}", list_to_string!(utils::regex_split(Regex::new(r"\W").unwrap(), str)));
}

Output
$ rustc RegEx3.rs error[E0432]: unresolved import `regex` --> RegEx3.rs:2:5 | 2 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `split` in this scope --> RegEx3.rs:6:48 | 6 | println!("Split 1: {}", utils::list_to_string(split(str,Regex::new(r" ").unwrap()))); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `split` on `&str` | 6 - println!("Split 1: {}", utils::list_to_string(split(str,Regex::new(r" ").unwrap()))); 6 + println!("Split 1: {}", utils::list_to_string(str.split(Regex::new(r" ").unwrap()))); | error[E0425]: cannot find function `split` in this scope --> RegEx3.rs:7:48 | 7 | println!("Split 2: {}", utils::list_to_string(split(str,Regex::new(r"[eo]").unwrap()))); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `split` on `&str` | 7 - println!("Split 2: {}", utils::list_to_string(split(str,Regex::new(r"[eo]").unwrap()))); 7 + println!("Split 2: {}", utils::list_to_string(str.split(Regex::new(r"[eo]").unwrap()))); | error[E0425]: cannot find function `split` in this scope --> RegEx3.rs:8:48 | 8 | println!("Split 3: {}", utils::list_to_string(split(str,Regex::new(r"\\s").unwrap()))); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `split` on `&str` | 8 - println!("Split 3: {}", utils::list_to_string(split(str,Regex::new(r"\\s").unwrap()))); 8 + println!("Split 3: {}", utils::list_to_string(str.split(Regex::new(r"\\s").unwrap()))); | error[E0425]: cannot find function `split` in this scope --> RegEx3.rs:9:48 | 9 | println!("Split 4: {}", utils::list_to_string(split(str,Regex::new(r"\\W").unwrap()))); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `split` on `&str` | 9 - println!("Split 4: {}", utils::list_to_string(split(str,Regex::new(r"\\W").unwrap()))); 9 + println!("Split 4: {}", utils::list_to_string(str.split(Regex::new(r"\\W").unwrap()))); | error: aborting due to 5 previous errors Some errors have detailed explanations: E0425, E0432. For more information about an error, try `rustc --explain E0425`.

Questions

Projects

More ★'s indicate higher difficulty level.

References