views:

200

answers:

4

How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?

+1  A: 

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...

annakata
No patterns for my cases, I think. I'm surprised this simple question is so tricky for regex.
Ricky
the problem is expressing logic
annakata
+2  A: 
^(?:(?!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.

Tim Pietzcker
+1  A: 

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.

wds
You need to anchor the whole regex, not just the lookahead. That means putting the `^` *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).
Alan Moore
Well I don't know. As I look at it now I wrote it as "if a line does not contain strB, match strA". Still seems correct to me, since we're matching line by line. I think. Reading old regexes makes my brain hurt.
wds
+1  A: 

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.

Alan Moore