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\dwhich 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 replace strings using Rust regex?
- How to make regex case insensitive in Rust?
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- Regex example to match multiline string in Rust?
- How to ignore case in Rust regex?
- How to get a capture group using Rust regex?
- How to get all matches from a Rust regex?
More of Rust
- How to use regex lookahead in Rust?
- How to use Unicode in a regex in Rust?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- Example of yield_now in Rust?
- How to map a Rust slice?
- How to check for equality between Rust slices?
- How can I use a hashmap as a global variable in Rust?
- How to iterate over a Rust HashMap?
- How to use a generator map in Rust?
See more codes...