How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With