0

1) drug A; 2) drug B; 3) drug C - ex; 4) drug D

How can only the drug that contains the "- ex" string be selected using a regular expression?

;.*?- ex

gives the following (greedy) result:

1) drug A ; 2) drug B; 3) drug C - ex; 4) drug D

1 Answers1

1

You could start the match by matching one or more digits \d+ followed by ) and use a negated character class [^;] matching any char except a ;

The word boundaries \b prevent the word characters being part of a larger word.

\b\d+\)[^;]*- ex\b

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70