From e4016739aec4f6487edec2f1e8aeed48159f1ff1 Mon Sep 17 00:00:00 2001 From: Noah Pendleton <2538614+noahp@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:10:26 -0400 Subject: [PATCH] Add a test case and doc for `${1}` group reference --- README.md | 4 ++++ src/replacer.rs | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e04543..d19069b 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,10 @@ ruplacer '(\w+), (\w+)' '$2 $1' (note the use of single quotes to avoid any processing by the shell) +`${1}` and `${2}` are also supported as an alternative to `$1` and `$2`, see +[reference +here](https://docs.rs/regex/1.5.5/regex/struct.Regex.html#replacement-string-syntax). +Useful if the replacement string is immediately adjacent to literal text. If you don't want the pattern to be used as a regex, use the `--no-regex` command line flag. diff --git a/src/replacer.rs b/src/replacer.rs index b26b54c..b7fa110 100644 --- a/src/replacer.rs +++ b/src/replacer.rs @@ -362,9 +362,10 @@ mod tests { fn test_regex_with_substitutions() { let input = "first, second"; let regex = Regex::new(r"(\w+), (\w+)").unwrap(); - let query = Query::regex(regex, r"$2 $1"); + // ensure ${1}text syntax works as expected + let query = Query::regex(regex, r"$2 ${1}third"); let replacement = replace(input, &query).unwrap(); - assert_eq!(replacement.output(), "second first"); + assert_eq!(replacement.output(), "second firstthird"); } #[test]