V. Terte V. Terte - 1 year ago 70
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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download