Regex 正则表达式匹配字符不在2个特定字符或单词之间

Regex 正则表达式匹配字符不在2个特定字符或单词之间,regex,Regex,我试图让这个正则表达式工作,但我只成功了一半 到目前为止,我可以做到这一点: 匹配每个“;”这不是在“[]”之间 使用此正则表达式:;(?![^\[]*\]) 但现在我的问题来了。。。我还应补充这一限制: 匹配每个“;”这不是介于“[”和“]”或“XXX”和“ZZZ”之间 例如,在本文中: aaa **;** bbb **;** [ ccc *;* ddd ] eee **;** XXX qweasd *;* qwesad ZZZ 第三个也是最后一个“;”不应匹配此模式似乎有效: ;(?![^

我试图让这个正则表达式工作,但我只成功了一半

到目前为止,我可以做到这一点:

匹配每个“;”这不是在“[]”之间

使用此正则表达式:
;(?![^\[]*\])

但现在我的问题来了。。。我还应补充这一限制:

匹配每个“;”这不是介于“[”和“]”或“XXX”和“ZZZ”之间

例如,在本文中:

aaa **;** bbb **;** [ ccc *;* ddd ] eee **;** XXX qweasd *;* qwesad ZZZ

第三个也是最后一个“;”不应匹配此模式似乎有效:

;(?![^[]*\])(?!((?!XXX).)*ZZZ)
说明:

;                   match a semicolon
(?![^[]*\])         assert that we cannot look forward and see a ] without also seeing
                    an opening [
                       -> this implies that the semicolon is not in between []
(?!((?!XXX).)*ZZZ)  assert that we cannot look forward and see ZZZ without
                    first seeing XXX
                       -> this implies that the semicolon is not in between XXX and ZZZ
(?:             # non-capturing parenthesis
    \[[^][]+\]  # anything between [ and ]
    |           # or
    XXX.+?ZZZ   # XXX ... ZZZ
)(*SKIP)(*FAIL) # all of this should be skipped
|               # ... or ...
;               # match a ;

请注意,此解决方案假定您不会在任何位置嵌套
[]
方括号,也不会嵌套XXX…ZZZ,而只是一个级别。

如果
(*SKIP)(*FAIL)
是一个选项(
PHP
PyPi Regex
之类),您可能会接受

(?:\[[^][]+\]|XXX.+?ZZZ)(*SKIP)(*FAIL)|;
看。
说明:

;                   match a semicolon
(?![^[]*\])         assert that we cannot look forward and see a ] without also seeing
                    an opening [
                       -> this implies that the semicolon is not in between []
(?!((?!XXX).)*ZZZ)  assert that we cannot look forward and see ZZZ without
                    first seeing XXX
                       -> this implies that the semicolon is not in between XXX and ZZZ
(?:             # non-capturing parenthesis
    \[[^][]+\]  # anything between [ and ]
    |           # or
    XXX.+?ZZZ   # XXX ... ZZZ
)(*SKIP)(*FAIL) # all of this should be skipped
|               # ... or ...
;               # match a ;

@第四个分号不是,但是第三个分号在
[
]
@TimBiegeleisen你说得对,我错过了一个,你在这里使用哪种编程语言<代码>(*SKIP)(*FAIL)可能是一个选项。在大多数情况下,您可以在不需要查找的情况下执行此操作。什么是编程语言,最终的结果是什么?您是否删除/替换/提取/计数出现次数/拆分?@Mandy8055是的,我可以这样做,但我认为这样做会使模式更难阅读,而且也不清楚这样做是否会提高性能。@Mandy8055不需要,解决方案没有帮助,因为它
即使它不在
XXX
ZZZ
[
]
@WiktorStribiżew
之间,解决方案也不会有帮助,因为
。。。如果OP可以声明括号将始终保持平衡,并且
XXX
将永远不会出现,除非稍后匹配
ZZZ
。哇,伙计们,你们的响应非常快,我刚刚做了一些测试,这似乎对我有效。它应该都在一个级别上,没有嵌套[]。非常感谢你。