0

I am trying to check a string using regex in PHP. if the string has a character "?" any where before a word then the regex will fail but if "?" is any where after the word then regex will pass. Like, my word is "world" and string is "Hi All? Hello World, How are you". Below are the scenarios.

  1. "Hi? All Hello World, How are you". will fail
  2. "Hi All? Hello World, How are you". will fail
  3. "Hi All Hello? World, How are you". will fail
  4. "Hi All Hello World, How? are you". will pass
  5. "Hi All Hello World, How are? you". will pass
  6. "Hi All Hello World, How are you?". will pass
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

If you want to prevent a ? anywhere before any occurrence of world, then a negative lookahead assertion will do the trick:

^(?is)(?!.*\?.*world).*

(?is) turns on case-insensitive matching and allows the dot to match any character including line breaks.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561