Multiline regular expression search in Visual Studio Code

前端 未结 5 1052
栀梦
栀梦 2020-12-05 09:43

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 endi

相关标签:
5条回答
  • 2020-12-05 09:59

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

    result of "Replace all":

    0 讨论(0)
  • 2020-12-05 10:00

    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 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.

    .

    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.

    0 讨论(0)
  • 2020-12-05 10:00

    The reason on this behavior is very simple.

    Multiple line search isn't implemented yet.

    see: Support multi-line search for Global search

    0 讨论(0)
  • 2020-12-05 10:06

    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

    0 讨论(0)
  • 2020-12-05 10:13

    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.

    0 讨论(0)
提交回复
热议问题