rustHow to get all matches from a Rust regex?
To get all matches from a Rust regex, you can use the find_iter
method on a Regex
object. This method returns an iterator of Match
objects, which can be used to access the matched text.
Example code
let re = Regex::new(r"\d+").unwrap();
let text = "123 456 789";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
Output example
123
456
789
Code explanation
Regex::new(r"\d+")
: creates a newRegex
object from the given regular expression.find_iter(text)
: returns an iterator ofMatch
objects from the given text.m.as_str()
: returns the matched text as a string.
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 replace strings using Rust regex?
- How to get a capture group using Rust regex?
- How to parse a file with 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?
More of Rust
- How to replace a capture group using Rust regex?
- How to implement PartialEq for a Rust HashMap?
- How to use a tuple as a key in a Rust HashMap?
- How to replace all matches using Rust regex?
- How to calculate the inverse of a matrix in Rust?
- How to convert Rust bytes to hex?
- How do I convert a string into another type in Rust?
- How to split a string with Rust regex?
- How to get the length of a Rust HashMap?
- How do I declare a variable with a type in Rust?
See more codes...