Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配中间值不在列表中?_Regex_Regex Negation - Fatal编程技术网

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$

详情:

  • ^
    -字符串的开头
  • 1
    -a
    1
    char
  • (?!(?:this | is | a | demo)2$)
    -如果存在
    this
    is
    a
    demo
    ,后跟
    2
    ,且字符串结尾紧跟当前位置右侧,则会导致匹配失败
  • *
    -除换行符以外的任何零个或多个字符,尽可能多
  • 2
    -a
    2
    char
  • $
    -字符串结束

中间不仅仅是一个字符,我已经更改了示例,您可以只使用一个字符吗look@distinct同样的技术,只需使用一组多角色备选方案,
^1(?(?:这是一个演示)2$).*2$
这个问题不再不清楚。