否定向前看正则表达式 [英] Negative Look Ahead Go regular expressions
问题描述
以下正则表达式: BBB(((?!BBB))。 )*)EEE
http:// rubular。 com / r / Zw1vopp1MF
然而,在Go中我得到:
error parsing regexp:无效或不支持的Perl语法:`(?!`
任何替代方案?
由于技术原因,不支持负向预测,具体原因是它与O(n)请参阅 golang-nuts小组讨论了解相关信息,以及通配表达式匹配中的警告部分。
您可以表达您所描述的正则表达式,而不会有负面情况:
BBB([^ B] | B [^ B] | BB [^ B])* EEE
下面是一个示例来演示:
<$ p
$ func main (){
re:= regexp.MustCompile(`BBB([^ B] | B [^ B] | BB [^ B])* EEE`)
fmt.Printf(%#v \ n,re.FindAllString(BBB EEE BBB..BB ... EEE,-1))
}
I'm trying to use negative look aheads in Go:
The following regular expression: BBB(((?!BBB).)*)EEE
http://rubular.com/r/Zw1vopp1MF
However in Go I get:
error parsing regexp: invalid or unsupported Perl syntax: `(?!`
Are there any alternatives?
Negative lookahead isn't supported for technical reasons, specifically because it conflicts with the O(n)-time guarantees of the library. See the golang-nuts group discussion about this, as well as the Caveats section in Regular Expression Matching in the Wild.
You can express the regular expression you've described without negative lookahead:
BBB([^B]|B[^B]|BB[^B])*EEE
Here's an example to demonstrate:
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`BBB([^B]|B[^B]|BB[^B])*EEE`)
fmt.Printf("%#v\n", re.FindAllString("BBB EEE BBB..BBB...EEE", -1))
}
这篇关于否定向前看正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!