我有这样的一行:
Word (mother) word 33 (453) word word 444 (4) word
结果应该是:(mother) (453) (4)
我想保留括号并删除行中的其余单词。我尝试了这个正则表达式,但效果不太好 :(
搜索:\([^!(]*?\)|\(|\)
替换为:\1
答案1
- Ctrl+H
- 找什么:
(?:^|\G)(?:\h*\w+\h*)+(\(\w+\)\h*)|(?:\h*\w+)*$
- 用。。。来代替:
$1
- 检查环绕
- 检查正则表达式
- Replace all
解释:
(?:^|\G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
\h* # 0 or more horizontal spaces
\w+ # 1 or more word characters
\h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
\(\w+\) # 1 or more word characters surounded by parenthesis
\h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
\h*\w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
给定示例的结果:
(mother) (453) (4)
答案2
提取带有括号的单词的简单正则表达式是:
\(([^)]+)\)
答案3
首先替换
編輯:
.*?(\(.*?\))
和
\1
然后更换
^(.*\)).*
和
\1
最终输出
(mother)(453)(4)