正则表达式查找和替换不介于()之间的文本 [英] Regex Find and Replace Text Not Between ()
问题描述
我有一个Sublime文本文件,看起来与此类似
I have a text file in Sublime text that looks similar to this
line 1
line 2
line 3
(line 4) hello (line6)
line 5 line 5
line 6 (line 6)
我要删除括号(:)之间的所有文本,同时保留括号内的内容.
I want to remove all text that isn't between brackets: () while keeping what is within the brackets.
我写了这个正则表达式,但是它选择了方括号内而不是外部的所有内容.
I wrote this regex but it selects everything inside the brackets rather than outside.
\([^)]*\)
有人知道正确的正则表达式可以解决这个问题吗?
Does anyone know the correct regex to solve this.
谢谢
更新:regex不会匹配同一行上的第二次出现.
Update: regex wont match second occurence on same line.
推荐答案
匹配以下任一项:
- 从头到尾或从头到尾
- 不愿意在方括号之间
- 从括号到结尾
正则表达式:
^.*?(\(|$)|\).*?\(|\).*
请参见在线演示(包括带有3个带括号的额外示例行).
See live demo (including an extra sample line that has 3 bracketed terms).
替换为空白.
如果要在此操作后删除空白行,请用空白替换^\n
.
If you want to remove blank lines after this operation, replace ^\n
with blank.
这篇关于正则表达式查找和替换不介于()之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!