Jarod
Jarod

Reputation: 199

Regex lookahead why

For PCRE, what's the difference between the following two regexes? (?=<!--)([\s\S]*?-->) and (<!--[\s\S]*?-->)

The first one is to match HTML comments mentioned HERE

Upvotes: 0

Views: 59

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522254

These two patterns will match the same thing. Here is an explanation of the first pattern:

(?=<!--)         assert that what immediately follows is <!--
([\s\S]*?-->)    then capture everything, across lines if necessary,
                 until reaching the first -->

The second pattern does not use lookaheads, but rather just matches a single HTML comment:

(<!--[\s\S]*?-->)

Again, this pattern will match across lines.

I would expect both patterns to have a similar performance. What you choose would depend on which performs better for your data, the tool you use (not all regex engines support lookarounds), and which pattern you find easier to read.

Upvotes: 1

Related Questions