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

regexp: In ReplaceAllString, the expand parameter eats the word after it #17469

Closed
aquilax opened this issue Oct 16, 2016 · 2 comments
Closed

regexp: In ReplaceAllString, the expand parameter eats the word after it #17469

aquilax opened this issue Oct 16, 2016 · 2 comments

Comments

@aquilax
Copy link

@aquilax aquilax commented Oct 16, 2016

What version of Go are you using (go version)?

go version go1.7.1 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/aquilax/gvm/pkgsets/go1.7.1/global"
GORACE=""
GOROOT="/home/aquilax/gvm/gos/go1.7.1"
GOTOOLDIR="/home/aquilax/gvm/gos/go1.7.1/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build728208925=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

https://play.golang.org/p/TFupLpOwFA

package main

import (
    "regexp"
    "fmt"
)

func main() {
    var re = regexp.MustCompile(`\{(([^\}\{]*) )?PAGE_TITLE( ([^\}]*))?\}`)
    var str = `<h1 id="page-title">{PAGE_TITLE} </h1>`
    var substitution = `$2My new wiki$4`

    fmt.Println(re.ReplaceAllString(str, substitution))
}

Workaround is to add space around the expand parameters

What did you expect to see?

<h1 id="page-title">My new wiki </h1>

What did you see instead?

<h1 id="page-title"> new wiki </h1>

@0xmohit
Copy link
Contributor

@0xmohit 0xmohit commented Oct 16, 2016

This seems to be working as per documentation:

In the $name form, name is taken to be as long as possible: $1x is equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0.

$2My would expand to nothing. https://play.golang.org/p/UWwb_BvF1Q would give you the expected results.

(BTW, have you seen text/template?)

@aquilax
Copy link
Author

@aquilax aquilax commented Oct 16, 2016

Thank you @0xmohit did't notice this part of the documentation.
I'm converting some PHP code to go and at first iteration trying to match the code as it is. Eventually will switch to html/template
Thanks again 👍

@aquilax aquilax closed this Oct 16, 2016
@mikioh mikioh changed the title In ReplaceAllString, the expand parameter eats the word after it regexp: In ReplaceAllString, the expand parameter eats the word after it Oct 16, 2016
@golang golang locked and limited conversation to collaborators Oct 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.