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 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?
- How to get a capture group using Rust regex?
- How to split a string with Rust regex?
- How to replace strings using Rust regex?
- How to parse a file with Rust regex?
- How to use non-capturing groups in Rust regex?
- Regex example to match multiline string in Rust?
More of Rust
- Hashshet example in Rust
- How to use a tuple as a key in a Rust HashMap?
- How to get a capture group using Rust regex?
- How to split a string with Rust regex?
- How to replace strings using Rust regex?
- How to parse a file with Rust regex?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to get an entry from a HashSet in Rust?
- How to convert a Rust HashMap to a JSON string?
See more codes...