You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Underscore in replacement string treated as backspace #69
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]fnreplacement_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"}
Interesting. It seems that _ is considered part of the capture group name. Since a group with that name doesn't exist, it is replaced with an empty string.
Definitely a bug. Not entirely sure what the right fix is. What is the simplest thing we can do?
let re = Regex::new(r"(.)(.)").unwrap();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"
Ah I see, it looks like I only fixed the first half of this, which was to make capture group names that start with a number illegal. The latter half, which I forgot to do, is to fix expansion such that it treats capture group names starting with a number in the replacement correctly. Thanks for catching this!
Since the docs weren't updated either, I'm marking this as something to be fixed in a regex2 some day. Fixing this now would be a breaking change. We'll just have to live with the wart for the time being.
Ugh, this is an unfortunate mistake. The "longest possible name" behavior pessimizes replacement strings that use numeric group references, so simple cases like $1asd don't work without {}. There's no benefit to this behavior, as there can be no ambiguity: capture groups starting with numbers have been disallowed now.
It also differs from the behavior of all other regex engines I've used (Java, JavaScript, Python, .NET, IntelliJ IDEA, probably more), so no doubt will catch many people besides me by surprise.
@PeterlitsZo The issue here isn't "what should it do instead," but that changing it is a breaking change and I have no plans to do a breaking change release any time soon.
@PeterlitsZo The issue here isn't "what should it do instead," but that changing it is a breaking change and I have no plans to do a breaking change release any time soon.
Thanks for your reply. I just mean that MAYBE it is useful in regex2 in the future. I am sorry if I comment on the wrong issue.
Activity
BurntSushi commentedon Apr 13, 2015
Interesting. It seems that
_is considered part of the capture group name. Since a group with that name doesn't exist, it is replaced with an empty string.Definitely a bug. Not entirely sure what the right fix is. What is the simplest thing we can do?
huonw commentedon Apr 13, 2015
Define that the names of capture groups cannot start with numbers? (i.e. do something similar to Rust identifiers.)
Rewrite parser as part of new regex-syntax crate.
ekse commentedon Jan 17, 2019
This bug seems to still be there.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fa837d651980121ff786eae319965fbc
BurntSushi commentedon Jan 17, 2019
Ah I see, it looks like I only fixed the first half of this, which was to make capture group names that start with a number illegal. The latter half, which I forgot to do, is to fix expansion such that it treats capture group names starting with a number in the replacement correctly. Thanks for catching this!
BurntSushi commentedon Jun 9, 2019
Since the docs weren't updated either, I'm marking this as something to be fixed in a regex2 some day. Fixing this now would be a breaking change. We'll just have to live with the wart for the time being.
tests: clarify numbered group binding
intgr commentedon Jun 28, 2021
Ugh, this is an unfortunate mistake. The "longest possible name" behavior pessimizes replacement strings that use numeric group references, so simple cases like
$1asddon't work without{}. There's no benefit to this behavior, as there can be no ambiguity: capture groups starting with numbers have been disallowed now.It also differs from the behavior of all other regex engines I've used (Java, JavaScript, Python, .NET, IntelliJ IDEA, probably more), so no doubt will catch many people besides me by surprise.
PeterlitsZo commentedon Jun 2, 2023
I think using braces to invoke the non-just-number variable will be a better idea.
$1asd=${1}asd;$asd= Just an error;${as}= OK.BurntSushi commentedon Jun 2, 2023
@PeterlitsZo The issue here isn't "what should it do instead," but that changing it is a breaking change and I have no plans to do a breaking change release any time soon.
PeterlitsZo commentedon Jun 2, 2023
Thanks for your reply. I just mean that MAYBE it is useful in regex2 in the future. I am sorry if I comment on the wrong issue.