在这里使用 Notepad++ v7.9。
假设 R
是一个复杂的正则表达式模式。我现在想匹配 _R
, R_
, 或 _R_
但不是 R
独自的。如何在不明确写入的情况下执行此操作 (_R|R_|_R_)
?这需要将 R 写出 3 次,而且看起来很难看。
我能想到的最接近的是 _?R_?
但这也匹配 R
单独(实际上等同于它),这对我来说是误报。
中间是 (_?R_|_R_?)
但是 R
在这里再次重复,虽然少了一次。
最佳答案
您可以使用 If-Then-Else如下:
(_)?R(?(1)_?|_)
Demo .这使您可以编写
R
只有一次。故障:
(_)? # An optional capturing group that matches an underscore character.
R # Matches 'R' literally (replace it with your pattern).
(? # If...
(1) # ..the first capturing group is found,...
_? # ..then match zero or one underscore characters (optional).
| # Else (otherwise)...
_ # Match exactly one underscore character (required).
) # End If.
在 Notepad++ 中工作:关于正则表达式 "either/or"但不是 "neither",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64680322/