Search code examples
regextargetparentheses

Regex to select text between parentheses with a specific text as target


I try to build a Regex to select all text between parentheses with a specific text like as target.

Something like (*TARGET*). I found this regex here : Regular Expression to get a string between parentheses in Javascript But (2014) and (Format) are select also.

\(([^)]+)\)

-Sample

Number 473 (2014) (Format)(not_wanted-text1 TARGET not-wanted_text2).xxx

or

Number 473 (2014) (Format)(TARGET not-wanted_text2).xxx

or

Number 473 (2014) (Format)(not-wanted_text2TARGET).xxx

-Expected result

Number 473 (2014) (Format).xxx

Thanks


Solution

  • You only have to include the target string surrounded by character classes that exclude the closing parenthesis:

    \(([^)]*TARGET[^)]*)\)
    

    If you only need to replace the match, you don't need the capture group (you can remove it).