正则表达式匹配一个字符串并否定另一个

regex to match one string and negate the other

我需要一个正则表达式来匹配来自 Test<OKAY>Test< 字符串,它不应该匹配 Test<?>

Test<?>  // shouldn't match
Test<OKAY> // should match

我试过 Test<[^?] 但它匹配 Test<O 而不是 Test<OKAY>

中的 Test<

我该如何解决这个问题?

测试<(?!\?) 有效

这里使用负前瞻。

?! = 后面没有 \?

Test<之后,非问号字符需要正向先行,后跟>表示括号结束:

Test<(?=[^?]+>)

https://regex101.com/r/gxpq3E/1

使用前瞻:

Test<(?=OKAY>)

live demo