[SOLVED] Get only first first occurence (if exists) in string with RegEx

Issue

I have some troubles with a RegEx which don’t do what I expect. Can someone tell me where I’m wrong ?

Goal: Get the first words (if exists) finishing with "’s " (with a space at the end)

RegEx :

".*'s "

Test string and the expected result :

Amarok's Golovin's Road => Amarok's 
Arkouna Dream's Elowin Engel's => Arkouna Dream's 
Arkouna Dream's Elowin Engel's => Arkouna Dream's  (in this case, I added a space after Engel's)
Liubov's Niagara => Liubov's 
Urane Of Watson Lake => nothing

Problem: When I have 2 occurences of "’s ", it returns a wrong result.

Solution

You can use

^[A-Z][a-zA-Z]*(?:\s+[a-zA-Z]+)*'s(?!\S)
^\p{Lu}\p{L}*(?:\s+\p{L}+)*'s(?!\S)

See the regex demo. Details:

  • ^ – start of string
  • \p{Lu} – an uppercase letter
  • \p{L}* – zero or more letters
  • (?:\s+\p{L}+)* – zero or more repetitions of one or more whitespace chars followed with one or more letters
  • 's – an 's substring
  • (?!\S) – a right-hand whitespace boundary.

Answered By – Wiktor Stribiżew

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *