Regular Expressions
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
mod utils; use regex::Regex; fn main() { let s:&str = "Four score and seven years ago..."; println!("Match 1: {}", match(s,Regex::new(r"s.*e").unwrap())); println!("Match 2: {}", match(s,Regex::new(r"\\bs.*e\\b").unwrap())); println!("Match 3: {}", match(s,Regex::new(r"s...e").unwrap())); println!("Match 4: {}", match(s,Regex::new(r"b.d").unwrap())); let mut subgroups:Vec<String> = findFirst(s,Regex::new(r"(\\w+)\\s*(\\w+)").unwrap()); println!("Find First (with subgroups): {}", utils::list_to_string(subgroups)); subgroups = findFirst(s,Regex::new(r"bad match").unwrap()); println!("Find First (bad match): {}", utils::list_to_string(subgroups)); let mut matches:Vec<String> = findAll(s,Regex::new(r"\\w+").unwrap()); println!("Find All: (matches only){}", utils::list_to_string(matches)); matches = findAll(s,Regex::new(r"bad match").unwrap()); println!("Find All (bad match): {}", utils::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`.
mod utils; use regex::Regex; fn main() { let s:&str = "Four score and seven years ago..."; println!("Replace First 1: {}", replaceFirst(s,Regex::new(r"\\.\\.\\.").unwrap(),"!!!")); println!("Replace First 2: {}", replaceFirst(s,Regex::new(r"\\b...\\b").unwrap(),"???")); println!("Replace First 3: {}", replaceFirst(s,Regex::new(r"b.d").unwrap(),"???")); println!("Replace First 4: {}", replaceFirst(s,Regex::new(r"(\\w+) (\\w+)").unwrap(),"$2 $1")); println!("Replace All 1: {}", replaceAll(s,Regex::new(r"\\b...\\b").unwrap(),"???")); println!("Replace All 2: {}", replaceAll(s,Regex::new(r"(\\w+) (\\w+)").unwrap(),"$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`.
mod utils; use regex::Regex; fn main() { let str:&str = "Four score and seven years ago..."; println!("Split 1: {}", utils::list_to_string(split(str,Regex::new(r" ").unwrap()))); println!("Split 2: {}", utils::list_to_string(split(str,Regex::new(r"[eo]").unwrap()))); println!("Split 3: {}", utils::list_to_string(split(str,Regex::new(r"\\s").unwrap()))); println!("Split 4: {}", utils::list_to_string(split(str,Regex::new(r"\\W").unwrap()))); }
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
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]