-2

I need to flag sentences when there is text in parentheses but only if a certain word (myword) is present.

These are some possible options of the word appearing inside parentheses:

(random text myword)
(random text myword random text)
(myword random text)
(myword, random text)
(random text, myword random text)

The output should contain parentheses and text inside.

UPD: The instances above might be surrounded by text with parentheses, i.e. (random text) random text (random text myword) random text (random text).

aviss
  • 1,797
  • 4
  • 24
  • 47

1 Answers1

0

You might use \(.*myword.*\)

Demo

or for your update:

\([^(]*myword[^)]*\)

Demo

Jarod42
  • 190,553
  • 13
  • 166
  • 271
  • The problem with this solution is that I have some other text around `(random text myword)` which may contain parentheses too. – aviss May 20 '20 at 07:23
  • @aviss: Add another possibility. With regex, we have to be clear on what we want and what we reject. – Jarod42 May 20 '20 at 08:01
  • Hmm, have actually realised that it doesn't work with this: `(random text) random text (random text) random myword (random text)` when myword appears in random text. – aviss May 20 '20 at 10:36
  • I guess this should work - `\([^(|)]*myword[^)|(]*\)`? – aviss May 20 '20 at 10:43
  • inside `[]`, `|` is not the "or", but is the single char `'|'`. – Jarod42 May 20 '20 at 10:45