正则表达式:如何在 Visual Studio 中检索包含 strA 但不包含 strB 的所有行
如何在 Visual Studio 搜索框中检索文档中包含“strA”但不包含“strB”的所有行?
How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 Visual Studio 2012(及更新版本):
说明:
如果您还想要,您可能需要在正则表达式末尾添加
(?:\r\n)?
删除回车/换行以及行的其余部分。For Visual Studio 2012 (and newer versions):
Explanation:
You might want to add
(?:\r\n)?
at the end of the regex if you also want to remove the carriage returns/line feeds along with the rest of the line.对于 Visual Studio 2010(及以前的版本):
Visual Studio 搜索框有其自己的奇怪版本的正则表达式语法。此表达式按要求工作:
^
匹配行的开头。 (通常对于文本编辑器来说,没有“多行”选项;^
和$
始终在行边界匹配。).
匹配除以下字符之外的任何字符换行符。 (不太常见,似乎没有“单行”或“全点”模式可以让点匹配换行符。)~(...)
是“阻止匹配”构造,相当于(据我所知)其他响应者使用的否定前瞻((?!...)
)。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:
^
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 或其他简单的、更快的逻辑测试...
You'd use Negative lookarounds but the expression is very complex if you don't know the expected position (or even order) of the terms. Do you know the order or pattern?
Otherwise I'd advise you to use another tool which could just as easily loop (or list comp) through a file line by line and do inStr or Contains or other simple, faster, logical tests...
我假设搜索框实际上接受一般的正则表达式。使用负向先行:
您需要设置多行选项(
^
和$
在行的开头/结尾匹配)。如果您无法使用对话框选项进行设置,请尝试:但这可能是该引擎中的默认模式。
I'm going to assume the search box actually accepts general regular expressions. Using negative lookahead:
You'll need to set the multiline options (
^
and$
match at start/end of lines). If you can't set it using the dialog options, try:This is probably the default mode in that engine though.
这对我来说适用于 Visual Studio 2010:
^.+[^(strB)].+(strA).+[^(strB)].+$
This worked for me with Visual Studio 2010:
^.+[^(strB)].+(strA).+[^(strB)].+$