Multi-line regular expressions in Visual Studio

RegexVisual Studio

Regex Problem Overview


Is there any way to get Visual Studio to perform a regex replace across multiple lines (let the match cross line boundaries)? I know there are many editors I can use for this, but it seems strange that this feature has been left out of Visual Studio. Am I missing something?

Regex Solutions


Solution 1 - Regex

Regular expressions have changed in Visual Studio 2013. https://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.120).aspx

To match an expression over two lines the code would now be:

StartOfExpression.*\r?\n.*EndOfExpression

Solution 2 - Regex

Use the replace in files dialog Ctrl-Shift-H and the single line option (?s):

(?s)start.*end

finds

start
two
three
end

Singleline means: each file is treated as single line, dot . matches newline \n. Downside: you must use Find All and replace all, or replace by hand. Find next does not work.

For the non-modal dialog Ctrl-H and find next, use (.*\n)* to match any number of lines:

start(.*\n)*.*end

Either way, you can replace your findings with multiple lines by inserting \n.

Solution 3 - Regex

This works today in Visual Studio 2012:

fooPatternToStart.*(.*\n)+?.*barPatternToEnd

See how the (.*\n)+? part does the match across multiple lines, non-greedy.
fooPatternToStart is some regex pattern on your start line, while barPatternToEnd is your pattern to find on another line below, possibly many lines below...

Example found here.

Simple and effective :)

Note: before VS2012, the pattern that worked was: fooPatternToStart.(.\n)+@.*barPatternToEnd

Solution 4 - Regex

Note: this answer is using the regex syntax used in Visual Studio up to and including VS 2012. In VS 2013 and later, the regex syntax has changed.

You can include \n in the expression. As an example, here is a regex that I use to "clean" auto-generated SQL scripts from anything that is not a stored procedure (it will match text blocks that start with a line containing "Object: " followed by something that is not "StoredProcedure", then matching the following lines up to a line consists of the word "GO"):

/\*+ Object\::b:b~(StoredProcedure)(.*\n)#GO\n

Solution 5 - Regex

you may need to use \r\n at the end of your expression.

Solution 6 - Regex

Non-greedy multi-line any character capture, Visual Studio 2013+:

.*?\r?\n.*?

Greedy version in Giles Roberts's answer.

Solution 7 - Regex

For everyone coming here while searching for VS Code, I use this to match anything from script to anywhere with 2 newlines (newlines excluded):

script(.|\n)+?(?=\n\n)

replace script and \n\n to match everything between them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul KeisterView Question on Stackoverflow
Solution 1 - RegexGiles RobertsView Answer on Stackoverflow
Solution 2 - RegexStephan StammView Answer on Stackoverflow
Solution 3 - Regexscrat.squirrelView Answer on Stackoverflow
Solution 4 - RegexFredrik MörkView Answer on Stackoverflow
Solution 5 - RegexKengView Answer on Stackoverflow
Solution 6 - RegexMeowView Answer on Stackoverflow
Solution 7 - RegexAli TouView Answer on Stackoverflow