Regex match numbers - capture using parentheses
-
Regex
-
captures
-
( -
)
Cargo.toml
[package]
name = "regex-match-number"
version = "0.1.0"
edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.9.3"
Code
use regex::Regex;
fn main() {
let text = String::from("There is the number 23 and another number here: 19");
println!("{}", text);
let re = Regex::new(r"[0-9]+").unwrap();
let number = match re.captures(&text) {
Some(value) => value,
None => {
println!("No match");
return;
}
};
println!("Number of matches: {}", number.len());
println!("Full match: '{}'", &number[0]);
// match the number that comes after colon (:)
// but the match now includes the : as well
let re = Regex::new(r": [0-9]+").unwrap();
let number = match re.captures(&text) {
Some(value) => value,
None => {
println!("No match");
return;
}
};
println!("Full match: '{}'", &number[0]);
// Use parentheses to capture parts of the string
let re = Regex::new(r": ([0-9]+)").unwrap();
let number = match re.captures(&text) {
Some(value) => value,
None => {
println!("No match");
return;
}
};
println!("Full match: '{}'", &number[0]);
println!("Matched: '{}'", &number[1]);
}