Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test case and doc for ${1} group reference #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions src/replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down