How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?
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.
^(?!.*strB)(?!.*strC)(?!.*strD).*strA.*$
. –
Swale 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.
^~(.*strB).*strA~(.*strB)
. Or ~(strB).*strA.*~(strB)
. –
Jellybean 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:
(?!^.*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.
^
outside the lookahead and adding another .*
in front of strA
. But there's no need to anchor any part of the regex to the end of the line (i.e., the .*$
can go). –
Bornstein This worked for me with Visual Studio 2010:
^.+[^(strB)].+(strA).+[^(strB)].+$
© 2022 - 2024 — McMap. All rights reserved.