How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?
views:
200answers:
4You'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...
^(?:(?!strB).)*strA(?:(?!strB).)*$
should work. At least it does in RegexBuddy; don't know the Visual Studio regex engine.
Explanation:
^ # anchor the search at the start of the line
(?:(?!strB).)* # match any number of characters, asserting that is is not possible to match strB at each match
strA # then match strA
(?:(?!strB).)* # same as before
$ # 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.
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.
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.
pffft! I just noticed the question is over two months old. I'll post this answer anyway, since the others didn't address Visual Studio's oddities.