V. Terte V. Terte - 5 months ago 45
PHP Question

Regex PHP: to look ahead twice

Why this:


can't match this


Thank you for your answer! It was very kind of you!


Because lookaheads only assert, they do not consume characters... so basically you're saying,

match lol:


then look head for abc:

lol(abc) // matches

now look ahead for dfg

lol(dfg) // does not match

This is why it's not working. You need to consume the characters, if you want to check for both abc and abcdfg. If you only want to look ahead you can do:


This will match the strings

  • lolabc
  • loldfg
  • lolabcdfg

However it's not very useful. The following would be more appropriate


Which would match lol, optionally abc then optionally dfg.