Regex: How to retrieve all lines containing strA but not strB in Visual Studio
Asked Answered
Q

5

24

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

Qualls answered 25/11, 2009 at 6:59 Comment(1)
Are you doing this at a command line, or in some code? Which OS or language?Fulgent
S
45

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.

Stutz answered 25/11, 2009 at 8:26 Comment(1)
This worked. I had to extend to support multiple strings for exclusion, which worked out to be e.g. ^(?!.*strB)(?!.*strC)(?!.*strD).*strA.*$.Swale
B
18

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.

Bornstein answered 6/2, 2010 at 11:54 Comment(2)
FYI, as of Visual Studio 2012 the dotNet regex engine is now used for searches, hence the bizarre syntax should now be gone. Nice answer but just be aware of which version of VS you are using.Karalynn
Testing in Visual Studio 2010 SP1Rel, this answer must be modified if "strB" might be AFTER "strA". Try ^~(.*strB).*strA~(.*strB). Or ~(strB).*strA.*~(strB).Jellybean
A
1

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

Arbitrage answered 25/11, 2009 at 7:12 Comment(2)
No patterns for my cases, I think. I'm surprised this simple question is so tricky for regex.Qualls
the problem is expressing logicArbitrage
G
1

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.

Guildhall answered 25/11, 2009 at 8:28 Comment(2)
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).Bornstein
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.Guildhall
T
0

This worked for me with Visual Studio 2010:

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

Taxicab answered 24/9, 2014 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.