Regex: Match/Delete everything on a line except parenthesis (keep the parentheses and delete the rest of the words on the line)
I have this kind of line:
Word (mother) word 33 (453) word word 444 (4) word
The result should be: (mother) (453) (4)
I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(
Search: \([^!(]*?\)|\(|\)
Replace by: \1
3 Answers
- Ctrl+H
- Find what:
(?:^|\G)(?:\h*\w+\h*)+(\(\w+\)\h*)|(?:\h*\w+)*$ - Replace with:
$1 - check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|\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 lineResult for given example:
(mother) (453) (4) A simple regex to extract a word with its parenthesis is:
\(([^)]+)\) first replace
edited:
.*?(\(.*?\))with
\1then, replace
^(.*\)).*with
\1Final output
(mother)(453)(4) 2