rustHow do you match strings with regex in Rust?
Matching strings with regex in Rust is done using the regex crate. This crate provides a Regex type which can be used to match strings against a regular expression.
Example code
use regex::Regex;
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
assert!(re.is_match("2018-01-01"));
Output example
(no output)
Code explanation
use regex::Regex: imports theRegextype from theregexcrate.Regex::new(r"^\d{4}-\d{2}-\d{2}$"): creates a newRegexobject from the given regular expression..unwrap(): unwraps theResultreturned byRegex::newand panics if the regular expression is invalid.re.is_match("2018-01-01"): checks if the given string matches the regular expression.
Helpful links
More of Rust
- How to escape dots with regex in Rust?
- How to use binary regex in Rust?
- How to replace strings using Rust regex?
- How to escape a Rust regex?
- How to make regex case insensitive in Rust?
- How to replace a capture group using Rust regex?
- How to match a URL with a regex in Rust?
- How to use regex lookahead in Rust?
- How to ignore case in Rust regex?
- How to use regex captures in Rust?
See more codes...