3

I am working in Go, I have a text file in which I want to replace a text based on a regex, but it's not working as expected even when I already tested the regex here and it says that there's a match.

I made the basic example in play ground and I am getting the same result. I have 3 text files with the same label (//==start== and //==end==), it works for the first one, but no for the second and third. What can be avoiding the regex to replace correctly the text?

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

This is the code that I used, I pasted all the string because I want to be sure that it's not the one affecting me

package main

func main() {

    var re = regexp.MustCompile(Myregex)
    s := re.ReplaceAllLiteralString(originalString,"replaced")
    fmt.Println(s)

}

var Myregex = `\/\/==start==\n(.+\n)*\/\/==end==`

var originalString = `// @Author: someone
// @Date:   2018-01-23T16:46:09-04:00
// @Email:  [email protected]
// @Filename: _material.themes.scss
// @Last modified by:   Someone
// @Last modified time: 2018-01-23T18:40:39-04:00

@include angular-material-theme($theme);

.app-dark {
    @include angular-material-theme($dark-theme);
}

.app-pink {
    @include angular-material-theme($pink-theme);
}

//==start==
//==end==`
8
  • 1
    Please include the relevant code in your question body instead of linking to it. Commented Jan 26, 2018 at 19:33
  • @Adrian done! I added all the string due that I want to be sure that it's not the cause of the problem. Thanks Commented Jan 26, 2018 at 19:36
  • The text isn't the same as in the regex101 link, so the match doesn't exist in the Go example. You're also using pcre there rather than Go. You also probably need a multi-line flag. Commented Jan 26, 2018 at 19:38
  • @JimB my bad, I updated the content in regex101 Commented Jan 26, 2018 at 19:47
  • @SrednyMCasanova: again the text isn't identical, so the match doesn't work. Commented Jan 26, 2018 at 19:49

1 Answer 1

11

Hope this will help you

func main() {
    var re = regexp.MustCompile(Myregex)
    s := re.ReplaceAllString(originalString, "replaced")
    fmt.Println(s)
}

var Myregex = `//==start==\n.*\n//==end==`

See in action: https://play.golang.org/p/GITAdHOOQOg

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.