Celeb Glow
general | February 27, 2026

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

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 line

Result for given example:

(mother) (453) (4) 

enter image description here

A simple regex to extract a word with its parenthesis is:

\(([^)]+)\)

first replace

edited:

.*?(\(.*?\))

with

\1

then, replace

^(.*\)).*

with

\1

Final output

(mother)(453)(4)
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy