如何在 Visual Studio 搜索框中检索包含“strA”但不包含“strB”的文档的所有行?
5 回答
对于 Visual Studio 2012(和更新版本):
^(?!.*strB).*strA.*$
解释:
^ # Anchor the search at the start of the line
(?!.*strB) # Make sure that strB isn't on the current line
.*strA.* # Match the entire line if it contains strA
$ # Anchor the search to the end of the line
(?:\r\n)?
如果您还想删除回车符/换行符以及该行的其余部分,则可能需要在正则表达式的末尾添加。
For Visual Studio 2010 (and previous versions):
The Visual Studio search box has its own, bizarre version of regex syntax. This expression works as requested:
^~(.*strB).*strA
^
matches the beginning of a line. (Typically for a text editor, there's no "multiline" option; ^
and $
always match at line boundaries.)
.
matches any character except a newline. (Not so typically, there appears to be no "single-line" or "dot-all" mode that lets dots match newlines.)
~(...)
is the "prevent match" construct, equivalent (as far as I can tell) to the negative lookahead ((?!...)
) used by the other responders.
您将使用否定环视,但如果您不知道术语的预期位置(甚至顺序),则表达式非常复杂。你知道顺序或模式吗?
否则,我建议您使用另一个工具,它可以轻松地逐行循环(或列出 comp)文件并执行 inStr 或 Contains 或其他简单、更快、逻辑测试...
我将假设搜索框实际上接受一般的正则表达式。使用负前瞻:
(?!^.*strB.*$)strA
您需要设置多行选项(^
并$
在行的开头/结尾匹配)。如果您无法使用对话框选项进行设置,请尝试:
(?m)(?!^.*strB.*$)strA
不过,这可能是该引擎中的默认模式。
这对我使用 Visual Studio 2010 有效:
^.+[^(strB)].+(strA).+[^(strB)].+$