Multiline regular expression search in Visual Studio Code

Visual Studio-CodeVscode Settings

Visual Studio-Code Problem Overview


Multiline regular expression search doesn't work in VS Code version 1.27.2 .

Theoretically aaa(\n|.)*bbb should find string starting from aaa and ending bbb but it doesn't work. The solution mentioned here Multi-line regular expressions in Visual Studio Code doesn't work as well.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Multiline search is added in v1.29 released in November 2018. See multi-line search.

> VS Code now supports multiline search! Same as in the editor, a regex > search executes in multiline mode only if it contains a \n literal. > The Search view shows a hint next to each multiline match, with the > number of additional match lines. > > This feature is possible thanks to the work done in the ripgrep tool > to implement multiline search.

multiline search: October 2018 release notes


Multiline search is coming to the Find Widget with v1.38. See multiline find "pre-release" notes.

> Multi Line search in Find Widget > > The Find Widget now supports multiple line text search and replace. By > pressing Ctrl+Enter, you can insert new lines into the input box.

multiline find widget.

Odd that it is Ctrl+Enter in the Find Widget but Shift+Enter in the Search Panel (see Deepu's answer below). Shift+Enter has other functionality when the Find Widget is focused.

Solution 2 - Visual Studio-Code

yes, you could use regex for mutliple line search in VScode.

To find a multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)

aaa(.|\n)+?bbb

To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)

aaa(.|\n)+bbb

Solution 3 - Visual Studio-Code

Without using regex.

Multi-line search is now possible in vs code version 1.30 and above without using regex.

Type Shift+Enter in the search box to insert a newline, and the search box will grow to show your full multiline query. You can also copy and paste a multiline selection from the editor into the search box.

Example

Solution 4 - Visual Studio-Code

I have been looking for a quick way to do this, and I have come to the following:

start_text.*?(.|[\n])*?end_text

with start_text and end_text being the bounds of your multiline search.

breaking down the regex ".?(.|[\n])?":

  1. ".?" will match any characters from your start text to the end of the line. The "?" is there to ensure that if your end_text is on the same line the . wont just keep going to the end of the line regardless (greedy vs lazy matching)
  2. "(.|[\n])" means either a character\whitespace or a new line
  3. "*?" specifies to match 0 or more of the expression in the parentheses without being greedy.

Examples:

  • <meta.*?(.|[\n])*?/> will match from the beginning of all meta tags to the end of the respective tags
  • <script.*?(.|[\n])*?</script> will match from the beginning of all script tags to the respective closing tags

Warning:

Using .*?(.|[\n])*? with improperly or partially filled in start_text or end_text might crash VS Code. I suggest either writing the whole expression out (which doesn't cause a problem) or writing the start and end text before pasting in the regex. In any case, when I tried to delete parts of the starting and ending text VS Code froze and forced me to reload the file. That being said, I honestly could not find something that worked better in VS Code.

Solution 5 - Visual Studio-Code

You can find and replace in multiple lines by using this simple regex : StringStart\r\nStringEnd

For example

public string MethodA(int x)
{ 
    var user;  
}

public string MethodB(string y)
{
    var user;  
}

public string MethodC(int x)
{ 
     var user;  
}

public string MethodD(float x)
{ 
     var user;  
}

If you want to replace the name of user variable with customer along with method parameter name to user but only for the int ones.

Then the regex to find will be : int x)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar user

and regex to replace will be : int user)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar customer

See for reference link

Solution 6 - Visual Studio-Code

I had a similar issue, this works better for me:

aaa[.\n\r\t\S\s]*bbb

This includes carriage return (\r), new line (\n), tab (\t), any whitespece (\s) and any non whitespace (\S). There seems to be some redundancy putting "." and "\S" together, but it doesn't work without both in my case.

Solution 7 - Visual Studio-Code

No regex way: you can copy multiline text and paste it in "Find in files" form:

enter image description here

result of "Replace all":

enter image description here

Solution 8 - Visual Studio-Code

The reason on this behavior is very simple.

Multiple line search isn't implemented yet.

see: Support multi-line search for Global search

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
QuestionmariszoView Question on Stackoverflow
Solution 1 - Visual Studio-CodeMarkView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeHui ZhengView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeDeepu ReghunathView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeAlex StoyanovView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeKartik Deep SagarView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeEmeeusView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeRuslan K.View Answer on Stackoverflow
Solution 8 - Visual Studio-CodemariszoView Answer on Stackoverflow