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 replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to match whitespace with a regex in Rust?
- How to use Unicode in a regex in Rust?
- How to get a capture group using Rust regex?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to use non-capturing groups in Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex to match a group in Rust?
More of Rust
- How to use regex to match a double quote in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to calculate the inverse of a matrix in Rust?
- How to use groups in a Rust regex?
- How to use a tuple as a key in a Rust HashMap?
- How to create a Rust regex from a string?
- How to add matrices in Rust?
- How to get an element from a HashSet in Rust?
See more codes...