0

我已经尝试了几个小时来解决这个问题。假设有一个字符串

"hello Exclude1 4:32  test test Exclude2 5:23 hello 2:19 some more text 42:3 more text"

我正在尝试创建一个仅匹配但 忽略2:1942:3Exclude1 4:32Exclude2 5:23

即如果 4:32 或 n:nn 前面有 Exclude1 或 Exclude2,则不匹配。谢谢

4

3 回答 3

8

您可以使用否定的lookbehind来忽略某个其他表达式之前的表达式:

(?<!Exclude1 )(?<!Exclude2 )\d+:\d+

这将匹配##:##没有立即出现在Exclude1 或之前的那个Exclude2 

演示:正则表达式| C# 代码示例

于 2013-01-30T21:58:07.810 回答
0
 (?<!a)b for example matches b which is not preceded by a, you can
 easily adapt it to your needs.
于 2013-01-30T21:58:31.013 回答
0

您是否尝试过类似的方法:

(?<!Exclude\d )\d+:\d+

于 2013-01-30T21:58:54.333 回答