Regex in Rust

Get to know regular expression in Rust.

First import crate “Regex” to “Cargo.toml” file.

regex = "1.4.2"

Run some test

use regex::Regex;
let re = Regex::new(r"first: \d+").unwrap();
assert_eq!(re.replace(", offset: 0, first: 20, filter: $filter","first: 1"),", offset: 0, first: 1, filter: $filter");

Here we find the string that match pattern r”first: \d+” . And will try to replace the match text with other value. In this case it is “first: 1”.

Hope it helps.

~~ PEACE ~~

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store