Paul Keister
Paul Keister

Reputation: 13077

Multi-line regular expressions in Visual Studio

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?

Upvotes: 107

Views: 44275

Answers (7)

Stephan Stamm
Stephan Stamm

Reputation: 1153

Use (.*\n)*? to skip zero or more lines between your expressions.

start(.*\n)*?end

finds

start
two
three
end

? is the non-greedy operator, used to skip as few lines as possible.

If end is not the first word in the line, add .* to match the extra characters. I.e.: start(.*\n)*?.*end finds

start
two
three
four end end

If you only want to replace until the first end, add another non-greedy operator: start(.*\n)*?.*?end.

Historic: In Visual Studio 2017 (and early versions of 2019) you can also use the single line option in the Find and Replace dialog Ctrl-Shift-F like this:

(?s)start.*end

For more see the version history of this answer.

Upvotes: 41

Meow
Meow

Reputation: 180

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

.*?\r?\n.*?

Greedy version in Giles Roberts's answer.

Upvotes: 2

Ali Tou
Ali Tou

Reputation: 2205

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.

Upvotes: 2

Fredrik Mörk
Fredrik Mörk

Reputation: 158319

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

Upvotes: 19

Giles Roberts
Giles Roberts

Reputation: 6883

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

Upvotes: 93

scrat.squirrel
scrat.squirrel

Reputation: 3826

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

Upvotes: 27

Keng
Keng

Reputation: 53101

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

Upvotes: 4

Related Questions