gpt4 book ai didi

C# 正则表达式不包括一个字符 (!)

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:28 25 4
gpt4 key购买 nike

下面的正则表达式运行良好:

\bG1\b

但我想修改它以在前面带有 ! 的情况下不查找 G1 事件。例如:

='Plan1'!G1            --> NOT match!
=G1*G2 --> MATCH!
='Plan2'!A1+'Plan2'!G1 --> NOT match!

这个正则表达式怎么会?

最佳答案

你需要一个 negative lookbehind :

(?<!subexpression)
where subexpression is any regular expression pattern. For a match to be successful, subexpression must not occur at the input string to the left of the current position. However, any substring that does not match subexpression is not included in the match result.

所以,使用

(?<!!)\bG1\b
^^^^^^

参见 regex demo

enter image description here

关于C# 正则表达式不包括一个字符 (!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38721135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com