Help create a regular expression that is in the sentence:

Well, maybe I shouldn't be dating such an old man.

Would find all the words, but only those in which there is no '

I tried:

[a-zA-Z]+[^'] but finds shouldn ' t

[a-zA-Z]+(?!') but also finds should n't and also shouldn' t

  • Php Java? .NET? - Wiktor Stribiżew
  • @ WiktorStribiżew notepad ++ - Letfar

2 answers 2

 /(?<![az]')\b[az]+\b(?!'[az])/i 

\b[az]+\b - searches for a word consisting of the letters az and AZ;
(?<![az]') - checks that there is no single quote before the word found, and before it is az or AZ;
(?!'[az]) - checks that the found word is not followed by a single quote, and then az or AZ.

PS Solution for php. PPS Test https://regex101.com/r/lb4Cle/1

    It is enough to make such an expression

     /[a-zA-Z]+'[a-zA-Z]+/ 

    If, by default, the search is greedy

    Test: https://regex101.com/r/lb4Cle/2