Search code examples
pythonregexpython-re

Match first substring option out of several options in a string


I have the string

  • string = 'fail alm alarm' and
  • a set of substrings substrings=[norm,alm,fail] that I want to match.

However I want norm and alm to have a higher matching priority than fail.

I wrote the pattern=r'norm|alm|fail'

thinking that since norm and alm appear before fail they would be given priority but
re.search('norm|alm|fail', 'fail alm alarm') returns

<re.Match object; span=(20, 24), match='fail'>

But I would like it to return alm.

Thanks in advance for any help


Solution

  • The order in the regex only influences the order in which the regex engine tries to find a match at the current position.

    To achieve what you want, you'll need to look ahead and check there is no "norm" or "alm" when going for "fail". The pattern for that would be:

    norm|alm|fail(?!.*(?:norm|alm))