英文:
VS Code Regex Search and Replace - whitespace removal between global search of two objects (if and return)
问题
我想删除筛选搜索结果中两个关键词之间的行:
这是我在VS Code中搜索的正则表达式 - ^(?![ \t]*//).*^.*\bif\b.*$\n\n(?![ \t]*//).*^.*\breturn\b.*$
我想删除if和return之间的行,但保留代码,只删除两个关键词之间的行。我知道可以使用$0
保留结果,但无法弄清楚下一步,希望能得到帮助!
以下是我想要在结果中的示例:
if o.R == nil {
return nil
}
以下是我不希望在结果中出现的示例:
if err != nil {
return err
}
还有以下不希望在结果中出现的示例:
if foreign.R == nil {
foreign.R = &portfolioBinOperationR{}
return nil
}
在尝试您的解决方案后,这是我的VSCode截图...我做错了什么地方?
英文:
I would like to remove the line in between this filtered search:
Here is the regex that I have in my Search in VS Code - ^(?![ \t]*//).*^.*\bif\b.*$\n\n(?![ \t]*//).*^.*\breturn\b.*$
I would like to get rid of the line in between the if and the return but keep the code, just remove the line in between. I know I can keep the result with the $0
but cant figure out the next part, any help would be much appreciated!
Here is an example of what I want in the results:
if o.R == nil {
return nil
}
Here are some examples of what I do not want in the results:
if err != nil {
return err
}
Also unwanted in the results:
if foreign.R == nil {
foreign.R = &portfolioBinOperationR{}
return nil
}
Here is the my VSCode after trying your solution...where am I going wrong?
答案1
得分: 1
按照 (if.*?\{)([\s\n]{2,}?)(?=^.*?return)
进行搜索,并用 $1\n
进行替换。请参考正则表达式 演示。
替换前
替换后
英文:
Search by (if.*?\{)([\s\n]{2,}?)(?=^.*?return)
and replace with $1\n
. See the regex demo.
Before Replacement
After Replacement
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论