Multiline regular expression search in Visual Studio Code
多行正则表达式搜索在VS Code 1.27.2版中不起作用。
理论上,
|.)*bbb
此处提到的解决方案Visual Studio Code中的多行正则表达式也不起作用。
多行搜索已添加到2018年11月发布的v1.29中。请参阅多行搜索。
VS Code now supports multiline search! Same as in the editor, a regex
search executes in multiline mode only if it contains a \
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.
多行搜索即将通过v1.38进入"查找小部件"。请参阅多行查找"预发行"说明。
Multi Line search in Find Widget
The Find Widget now supports multiple line text search and replace. By
pressingCtrl+Enter , you can insert new lines into the input box.
。
奇怪的是,它在"查找"小部件中是Ctrl + Enter,但在搜索面板中是Shift + Enter(请参阅下面的Deepu回答)。当"查找小部件"成为焦点时,Shift + Enter具有其他功能。
不使用正则表达式。
现在,vs代码版本1.30及更高版本中可以进行多行搜索,而无需使用正则表达式。
在搜索框中键入Shift + Enter以插入换行符,搜索框将扩大以显示完整的多行查询。您还可以从编辑器中复制多行选择并将其粘贴到搜索框中。
您可以使用以下简单的正则表达式在多行中查找和替换:StringStart \ r \ nStringEnd
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; } |
如果要将用户变量的名称替换为customer,将方法参数名称替换为user,而仅将int替换为user。
然后要查找的正则表达式将是:int x)\ r \ nEnterBlankSpacesHereToReachTheString {\ r \ nEnterBlankSpacesHereToReachTheStringvar用户
和要替换的正则表达式将是:int用户)\ r \ nEnterBlankSpacesHereToReachTheString {\ r \ nEnterBlankSpacesHereToReachTheStringvar客户
参见参考
没有正则表达式的方式:您可以复制多行文本并将其粘贴为"在文件中查找"形式:
"全部替换"的结果:
这种行为的原因很简单。
多行搜索尚未实现。
请参阅:支持多行搜索以进行全局搜索