// plain
To escape parentheses in a Rust regex, you can use a backslash (\
) before the parentheses. For example:
let re = Regex::new(r"\(test\)").unwrap();
This will create a regex that matches the literal string (test)
.
let re =
: This declares a variablere
of typeRegex
.Regex::new
: This is a static method of theRegex
type that creates a newRegex
from a string.r"\(test\)"
: This is the string passed toRegex::new
. Ther
indicates that it is a raw string, which means that backslashes are not treated as escape characters. The backslash before the parentheses escapes them, so that the regex matches the literal string(test)
.
group: rust-regex
onelinerhub: How to escape parentheses in a Rust regex?