163

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.

mariszo
  • 1,799
  • 2
  • 10
  • 12
  • 3
    The solution in [Multi-line regular expressions in Visual Studio Code](https://stackoverflow.com/questions/41150941/multi-line-regular-expressions-in-visual-studio-code) works. `aaa[\s\S\r]*bbb`, `aaa[\W\w]*bbb`, `aaa[^\r]*bbb` work. – Wiktor Stribiżew Jul 11 '20 at 16:07
  • Just as an example, just used `

    ((.|\n)*?)

    ` to find multiline paragraphs in HTML. The external parenthesis allows me to replace using $1. The internal is ignored for replacement purposes.
    – Toni Homedes i Saun Jul 31 '23 at 11:57

10 Answers10

159

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 8
    For those looking for a quick copy paste this is what you use to match multilines [\n\s\]+ – Guillermo Siliceo Trueba Jan 22 '19 at 17:56
  • Related question and answer https://stackoverflow.com/questions/54335945/vs-code-multiline-search-and-replace-for-html-xml-tags/54335946#54335946 – Ed of the Mountain Jan 23 '19 at 21:30
  • 57
    Use `(.*\n)+` to match one or more lines with or without content, .e.g, `(.*\n)+` for an entire document. – Magnus Lind Oxlund May 18 '19 at 16:26
  • 7
    I think this implicitly relying on the presence of a `\n` literal newline was a bad design decision. I would never have guessed that if i had not read your answer. Multiline or not is usually a separate flag/toggle and that's exactly how they should have implemented this imho: with an explicit toggle. One should not have to search the web for some VSCode-specific way to toggle multiline search. – Stijn de Witt May 13 '22 at 09:31
  • It also works in multiline mode if it contains something like `[\s\S\r]` – mbomb007 Jan 09 '23 at 15:20
  • does not work in VSCode 1.76.2 (mac) – Basti Mar 30 '23 at 06:26
  • @MagnusLindOxlund that means it will only catch the case where ` – rioV8 Apr 06 '23 at 06:34
  • If you want to capture multiline content use `((?:.|\n)*?)` equivalent to `(.*?)` with multiline flag enabled. Vscode devs should add a right click menu on top of the regex button to customize flags, because this hacks are not very readable – Madacol May 19 '23 at 11:39
  • As of Visual Studio Code, version 1.78.2 on Windows 10 Pro, "Shift + Enter" works the same way for me as "Ctrl + Enter", as long as the cursor is within the "Find" box or "Replace" box. So if you're more used to the standard "Shift + Enter" to line break, you can use that if it works – CreativiTimothy May 20 '23 at 18:48
  • `[\s\S]*` works well in macOS. – Martin Braun Jul 07 '23 at 19:54
103

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
Hui Zheng
  • 2,394
  • 1
  • 14
  • 18
38

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.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Alex Stoyanov
  • 481
  • 3
  • 2
  • 5
    Note that this will not capture the content between start and end. You'll want to wrap the regex in a capture group like `(.*?(.|[\n])*?)`. – brainbag Feb 03 '22 at 15:01
  • it works for searching but not for replacing.. i mean $1 works but $2 not.. like this: (xxx)(.|[\n])* – luky Jul 06 '22 at 08:32
  • This works when the text between `start_text` and `end_text` may include new line. Well done! – mnaa Mar 15 '23 at 09:06
20

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

Deepu Reghunath
  • 8,132
  • 2
  • 38
  • 47
1

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

SHR
  • 7,940
  • 9
  • 38
  • 57
1

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.

Emeeus
  • 5,072
  • 2
  • 25
  • 37
1

If you are willing to search JavaScript, TypeScript or JSON files I can recommend my VScode extension

It allows for formatting agnostic text search and structural code search

You can find it on codeque.co or at VSCode Marketplace

Your query could look like this

aaa$$mbbb

  • where $$m means optional multiline set of any characters

Make sure to use text mode for this query

CodeQue can make much more than that!

0

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

Ruslan K.
  • 1,912
  • 1
  • 15
  • 18
0

(.|\n)+? or [\s\S\r]* or [.\n\r\t\S\s]* may be understandable when viewed in isolation, but in an already complex regex expression, they can add that extra layer of complexity that makes the whole thing unmanageable.

On Windows, for files on the local disk, I find the best solution is to switch to using Notepad++. Not only does it handle multi-line out of the box, it also has a pleasant interface for multi-file search and replace, handles macros gracefully, and is quite light-weight. You can switch back to VScode as soon you have finished your regex changes. Personally, I deleted Notepad++ when I found VScode, but reinstalled it later when I found some of what Notepad++ had to offer was missing in VScode. Both are free to use! I'm sure there's an equivalent on the Mac.

Francis
  • 702
  • 9
  • 21
-3

The reason on this behavior is very simple.

Multiple line search isn't implemented yet.

see: Support multi-line search for Global search

mariszo
  • 1,799
  • 2
  • 10
  • 12