Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 956
Ricky Avatar asked Nov 25 '09 06:11

Ricky


People also ask

How do I find the regex code in Visual Studio?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

How do you use wildcards in regex?

In regular expressions, the period ( . , also called "dot") is the wildcard pattern which matches any single character. Combined with the asterisk operator . * it will match any number of any characters.

How do you escape in regex?

The \ is known as the escape code, which restore the original literal meaning of the following character. Similarly, * , + , ? (occurrence indicators), ^ , $ (position anchors) have special meaning in regex. You need to use an escape code to match with these characters.


1 Answers

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.

like image 197
Tim Pietzcker Avatar answered Sep 19 '22 15:09

Tim Pietzcker