Using positive-lookahead (?=regex) with re2

邮差的信 提交于 2019-11-28 13:24:17

According to the Syntax Documentation, this feature isn't supported:

(?=re) before text matching re (NOT SUPPORTED)

Also, from WhyRE2:

As a matter of principle, RE2 does not support constructs for which only backtracking solutions are known to exist. Thus, backreferences and look-around assertions are not supported.

You can achieve this with a simpler regexp:

re := regexp.MustCompile(`^(.+?)(?:baz)?$`)
sm := re.FindStringSubmatch("foo bar baz")
fmt.Printf("%q\n", sm)

sm[1] will be your match. Playground: http://play.golang.org/p/Vyah7cfBlH

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!