rustHow to match digits with regex in Rust?
Matching digits with regex in Rust can be done using the \d
character class. This character class matches any single digit from 0 to 9.
Example code
let re = Regex::new(r"\d").unwrap();
let text = "123";
for cap in re.captures_iter(text) {
println!("{}", &cap[0]);
}
Output example
1
2
3
Code explanation
Regex::new(r"\d")
: creates a new Regex object with the pattern\d
which matches any single digit from 0 to 9.captures_iter(text)
: returns an iterator over all the captures in the text.&cap[0]
: returns the first capture group, which in this case is the digit.
Helpful links
Related
- How to match whitespace with a regex in Rust?
- How to use non-capturing groups in Rust regex?
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to replace strings using Rust regex?
- How to replace all matches using Rust regex?
- How to parse a file with Rust regex?
- How to split a string with Rust regex?
- Regex example to match multiline string in Rust?
- How to ignore case in Rust regex?
More of Rust
- How to replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to split a string by regex in Rust?
- How to replace strings using Rust regex?
- How to replace all matches using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to split a string with Rust regex?
- How to use the global flag in a Rust regex?
- How to use Unicode in a regex in Rust?
See more codes...