-2

With regex, from the following sentence

my name is oscar my name is oscar my name is oscar my name is david my name is oscar

I would like to get the "oscar" immediately previous to david (in this case would be the 3rd "oscar")

I've tried many things but none of them have worked.

Any ideas? Thanks

Oscar
  • 407
  • 6
  • 16

1 Answers1

2
oscar(?=(?:(?!oscar).)*david)

Find an "oscar" such that it is followed by any number of characters that do not start an "oscar", followed by a "david".

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Amadan
  • 191,408
  • 23
  • 240
  • 301