正则表达式:如何在 Visual Studio 中检索包含 strA 但不包含 strB 的所有行

发布于 2024-08-12 00:33:43 字数 74 浏览 4 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晚风撩人 2024-08-19 00:33:43

对于 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 2012 (and newer versions):

^(?!.*strB).*strA.*$

Explanation:

^           # 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

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.

愛放△進行李 2024-08-19 00:33:43

对于 Visual Studio 2010(及以前的版本):

Visual Studio 搜索框有其自己的奇怪版本的正则表达式语法。此表达式按要求工作:

^~(.*strB).*strA

^ 匹配行的开头。 (通常对于文本编辑器来说,没有“多行”选项;^$ 始终在行边界匹配。)

. 匹配除以下字符之外的任何字符换行符。 (不太常见,似乎没有“单行”或“全点”模式可以让点匹配换行符。)

~(...) 是“阻止匹配”构造,相当于(据我所知)其他响应者使用的否定前瞻((?!...))。

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.

放肆 2024-08-19 00:33:43

您可以使用否定环顾,但如果您不知道,则表达式非常复杂术语的预期位置(甚至顺序)。 你知道顺序或模式吗?

否则,我建议您使用另一个工具,它可以轻松地逐行循环(或列出 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...

青朷 2024-08-19 00:33:43

我假设搜索框实际上接受一般的正则表达式。使用负向先行:

(?!^.*strB.*$)strA

您需要设置多行选项(^$ 在行的开头/结尾匹配)。如果您无法使用对话框选项进行设置,请尝试:

(?m)(?!^.*strB.*$)strA

但这可能是该引擎中的默认模式。

I'm going to assume the search box actually accepts general regular expressions. Using negative lookahead:

(?!^.*strB.*$)strA

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:

(?m)(?!^.*strB.*$)strA

This is probably the default mode in that engine though.

野鹿林 2024-08-19 00:33:43

这对我来说适用于 Visual Studio 2010:

^.+[^(strB)].+(strA).+[^(strB)].+$

This worked for me with Visual Studio 2010:

^.+[^(strB)].+(strA).+[^(strB)].+$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文