VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

huangapple go评论77阅读模式
英文:

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截图...我做错了什么地方?

英文:

VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)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! VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

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?
VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

答案1

得分: 1

按照 (if.*?\{)([\s\n]{2,}?)(?=^.*?return) 进行搜索,并用 $1\n 进行替换。请参考正则表达式 演示

替换前

VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

替换后

VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

英文:

Search by (if.*?\{)([\s\n]{2,}?)(?=^.*?return) and replace with $1\n. See the regex demo.

Before Replacement

VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

After Replacement

VS Code Regex Search and Replace – whitespace removal between global search of two objects (if and return)

huangapple
  • 本文由 发表于 2022年6月14日 05:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/72609377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定