Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline regular expression search in Visual Studio Code

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.

like image 457
mariszo Avatar asked Oct 04 '18 13:10

mariszo


People also ask

Can you use regex in Vscode search?

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.

How do you write a multiline in vs code?

Select the lines you want and then press: Windows: Shift + Alt + i. Mac: shift + option + i.

How do you select multiple occurrences in Vscode?

Multi-word: Ctrl+Shift+L / ⌘+Shift+L selects all instances of the current highlighted word.

What is multiline mode in regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.


Video Answer


3 Answers

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.

like image 123
Mark Avatar answered Oct 07 '22 06:10

Mark


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
like image 38
Hui Zheng Avatar answered Oct 07 '22 05:10

Hui Zheng


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.

like image 26
Alex Stoyanov Avatar answered Oct 07 '22 05:10

Alex Stoyanov