rust-lang / rust-lang/regex

Underscore in replacement string treated as backspace

Open
#69 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement regex2
Dominant language
Rust
Stars
4k
Forks
534
PR merge metrics
No merged PRs in 30d

Description

Using a _ in the replacement string of Regex::replace leads to unexpected behaviour. The _ seems to be treated as a backspace. The documentation should either make mention of this, or this seems to be a bug.

#[test]
fn replacement_with_underscore() {
    let re = regex!(r"(.)(.)");
    let s1 = re.replace("ab","$1-$2");
    let s2 = re.replace("ab","$1_$2");
    assert_eq!("a-b", &s1);
    assert_eq!("a_b", &s2); // Fails here "a_b" != "b"
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided replacement_with_underscore test and trace how Regex::replace parses replacement strings. Determine whether the underscore behavior is unintended or an undocumented rule, then verify the chosen resolution with a regression test or documentation update.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.