Search code examples
regexregexp-replace

Regex to remove parentheses and inner contents only if contents have certain words


Could someone please help me with a regex to remove parentheses and their inner contents only when there are certain words inside, regardless of position and case of these words?

For example, if my string has parentheses with the word banana or apple inside, these parentheses and their inner contents should be removed. However, if there are other parentheses that do not have these words inside them, they should not be removed.

Example: should remove parentheses that have either the word banana or apple:

  • "this is a string (bla bla banana bla bla) another set of parentheses (bla bla)"
  • "this is a string (banana bla bla) another set of parentheses (bla bla)"
  • "this is a string (bla blabanana) another set of parentheses (bla bla)"
  • "this is a string (bla BANANA ) another set of parentheses (bla bla)"
  • "this is a string ( apple bla) another set of parentheses (bla bla)"
  • "this is a string (blaAPPLE bla) another set of parentheses (bla bla)"

should become

  • "this is a string another set of parentheses (bla bla)"

Thank you very much!


Solution

  • Supposing you're using a regex to replace sub-strings that matches it, you can use: \([^)]*word[^)]*\)

    And replace matches with an empty string.

    With that regex, you find a block of parentheses that have inside the word, with any character after of before. Any character but a closed parentheses, that would mean the block already ended.