【发布时间】:2016-06-24 21:46:39
【问题描述】:
我有一个字符串
centenary
我只想匹配ten,前提是它前面没有cen。
到目前为止,我有这个正则表达式:
(([^c][^e][^n])|^)ten
在以下情况下返回 true tenary、blahtenary 和 false 为 ctenary、cetenary、centanary
package main
import (
"fmt"
"regexp"
)
func main() {
txt := "ctenary"
rx := `(([^c][^e][^n])|^)ten`
re := regexp.MustCompile(rx)
m := re.MatchString(txt)
fmt.Println(m)
}
【问题讨论】:
-
如果我正确地阅读了您的问题,您正在寻找一个前瞻替代方案。你可以试试
(?:^|[^n]|[^e]n|[^c]en)(ten)。 -
@Aaron 是的,我想匹配前面没有“cen”的“十”。我已经编辑了问题。
-
@SebastianProske 按预期工作!谢谢