Update
Go regexp
module does not support lookaheads because this package guarantees to run in O(n) time, and the authors did not find a way to introduce lookarounds without violating these constraints.
However, you may use different workarounds. For the current one, you can use the http://www.formauri.es/personal/pgimeno/misc/non-match-regex Web service that generates POSIX-compatible negated patterns. E.g. for somestring
, it generates a ^([^s]|s(s|o(s|m(s|es(omes)*(s|t(s|r(s|i(s|ns)))|o(s|ms)))))*([^os]|o([^ms]|m([^es]|e([^s]|s(omes)*([^ost]|t([^rs]|r([^is]|i([^ns]|n[^gs])))|o([^ms]|m([^es]|e[^s]))))))))*(s(s|o(s|m(s|es(omes)*(s|t(s|r(s|i(s|ns)))|o(s|ms)))))*(o((me?)?|mes(omes)*(t(r?|rin?)|o(me?)?)?))?)?$
regex, and in order to use it in your original regex, all you need is to replace the last (.*)
with (<part after ^>)
, i.e. the regex will look like
/[^/]*/[^/]*/(([^s]|s(s|o(s|m(s|es(omes)*(s|t(s|r(s|i(s|ns)))|o(s|ms)))))*([^os]|o([^ms]|m([^es]|e([^s]|s(omes)*([^ost]|t([^rs]|r([^is]|i([^ns]|n[^gs])))|o([^ms]|m([^es]|e[^s]))))))))*(s(s|o(s|m(s|es(omes)*(s|t(s|r(s|i(s|ns)))|o(s|ms)))))*(o((me?)?|mes(omes)*(t(r?|rin?)|o(me?)?)?))?)?)$
See the regex demo.
To make sure the regex only captures the part after third backslash, the first two .*
patterns are replaced with [^/]*
that match zero or more chars other than /
. (In the demo, I added \n
, too, to avoid matching across lines in the single multiline string demo).
Originally accepted answer
The anything/anything/somestring
should not be expressed as \/.*\/.*\/(.*)
. The first .*
matches up to the last but one /
in the string. You need to use a negated character class [^/]
(not the /
should not be escaped in Go regex).
Since RE2 that Go uses does not support lookaheads, you need to capture (as JimB mentions in the comments) all three parts you are interested in, and after checking the capture group #1 value, decide what to return:
package main
import (
"fmt"
"regexp"
)
func main() {
s := "anything/anything/somestring"
r := regexp.MustCompile(`^[^/]+/[^/]+/(.*)`)
val := r.FindStringSubmatch(s)
// fmt.Println(val[1]) // -> somestring
if len(val) > 1 && val[1] != "somestring" { // val has more than 1 element and is not equal to somestring?
fmt.Println(val[1]) // Use val[1]
} else {
fmt.Println("No match") // Else, report no match
}
}
See the Go demo
(?!somestring)
, which is what you need, is out. The only way around it is to use your regex\/.*\/.*\/(.*)
in a while loop. Each match, string compare group 1 withsomestring
.