Open a-h opened 4 years ago
/cc @rsc @matloob
I labeled this as Documentation, as I am not sure how we could change this without breaking real-world programs. I have also been caught by this before.
Thanks. This isn't something we can change today, so it has to be a documentation fix.
Change https://golang.org/cl/244625 mentions this issue: doc: improve re.ReplaceAllString documentation related to Expand part
I think the simplest change is to nudge people to use the more explicit syntax.
A lot of the time, people don't look at the examples in the docs, they just look at the text that pops up in their development environment, so how about this?
ReplaceAllString returns a copy of src, replacing matches of the Regexp with the replacement string repl. Inside repl, $ signs are interpreted as in Expand, for instance, ${1} represents the text of the first submatch, and ${xyz} represents the text of the submatch named xyz.
The problem with skipping the braces when referring to submatches is hinted at in the example code, but it's a bit too subtle, maybe adding a comment there would bring it to the attention. I don't think any more than that is needed.
// $1W is not a named submatch, so the replacement is with an empty slice.
fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
Rebased PR to the current master, can we review one more time and land it ?
Change https://go.dev/cl/446836 mentions this issue: regexp: improve ReplaceAllString documentation related to Expand part
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/WYmtk1Gi-T_4 https://play.golang.org/p/XAdw3-cgx0z
What did you expect to see?
Expected that the first capture submatch
(.)
resulted in the characterb
, so the result should have beencba
, according to the docs at https://golang.org/pkg/regexp/#Regexp.ReplaceAllStringWhat did you see instead?
I just got the character
c
, I expected to getcba
, because the replacement had been interpreted differently.When I dug into it to raise this report, I found this in the documentation on the
Expand
function:I didn't realise I was using the name form, I thought I was using the number form. This made it clearer that the regexp group was being interpreted as
${1a}
, not$1
, followed bya
.That means, I would need to use "c${1}a" as my replacement, which is quite unusual because I haven't seen another programming language that works this way, e.g. Node.js will return
cba
.sed
also works as I expected (although it uses\1
as the reference instead of$1
).It was surprising behaviour to me, and I spent a while debugging the program I was working on before I realised there was something else going on.
Ideally, the Go implementation would change to match other programming languages and text editors, i.e. after a dollar, if the first character is
[0-9]
, then it's a numeric reference up until the next non-numeric character, e.g.However, if that's not possible, then a documentation change might help.
$1
is brittle, it only works if your reference to the capture group is the whole replacement ($1
), or is followed by a space character ($1 a
), at the end of the replacement (a$1
) etc. So the documentation should use the long form by default.