Regex 正则表达式匹配中间值不在列表中?
我正在尝试匹配在第一个字符和最后一个字符之间不包含特定字符串的字符串。这些字符串以列表形式出现,如下所示:Regex 正则表达式匹配中间值不在列表中?,regex,regex-negation,Regex,Regex Negation,我正在尝试匹配在第一个字符和最后一个字符之间不包含特定字符串的字符串。这些字符串以列表形式出现,如下所示: this is a demo 可以有更多。我写过 1(?!this|is|a|demo).*2 这是。它只匹配1demo12,我也需要它匹配1demo2 更多测试用例: 1this2 # do not want match 1is2 # do not want match 1a2 # do not want match 1demo2
this
is
a
demo
可以有更多。我写过
1(?!this|is|a|demo).*2
这是。它只匹配1demo12
,我也需要它匹配1demo2
更多测试用例:
1this2 # do not want match
1is2 # do not want match
1a2 # do not want match
1demo2 # do not want match
1demoo2 # want match
1demo12 # want match
我尝试使用负前瞻
,但失败。您可以使用
^1(?(?:这是一个演示)2$)*2$
看
详情:
-字符串的开头^
-a1
char1
-如果存在(?!(?:this | is | a | demo)2$)
,this
,is
或a
,后跟demo
,且字符串结尾紧跟当前位置右侧,则会导致匹配失败2
-除换行符以外的任何零个或多个字符,尽可能多*
-a2
char2
-字符串结束$
^1(?(?:这是一个演示)2$).*2$
这个问题不再不清楚。