I have content I am trying to remove from a string
s:=`Hello! <something>My friend</something>this is some <b>content</b>.`
I want to be able to replace <b>content</b>
and <something>My friend</something>
so that the string is then
`Hello! this is some .`
So basically, I want to be able to remove anything between <.*>
But the problem is that the regex matches <something>My friend</something> this is some <b>content</b>
because golang is matching the first <
to the very last >
Go's regexp doesn't have backtracking so you can't use <(.*?)>.*?</\1>
like you would do in perl.
However if you don't care if the closing tag matches you can use:
<.*?/.*?>
Just saw your update, .*
is a greedy operator, it will match everything in between, you have to use non-greedy matching (aka .*?
).
*
is a greedy operator meaning it will match as much as it can and still allow the remainder of the regular expression to match. In this case, I would suggest using negated character classes since backreferences are not supported.
s := "Hello! <something>My friend</something>this is some <b>content</b>."
re := regexp.MustCompile("<[^/]*/[^>]*>")
fmt.Println(re.ReplaceAllString(s, ""))