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

Ricky picture Ricky · Nov 25, 2009 · Viewed 8.9k times · Source

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

Answer

Tim Pietzcker picture Tim Pietzcker · Nov 25, 2009

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.