[php-src] Issue #11866: PHP preg_match wrong matching behaviour for lookahead containing < character
| From: | jild | Date: | Thu, 03 Aug 2023 08:09:48 +0000 |
| Subject: | [php-src] Issue #11866: PHP preg_match wrong matching behaviour for lookahead containing < character | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245092@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11866
Author: jild
### Description
The following code:
```php
preg_match(
'/<text:p[^>\/]*>(?:.(?!<text:p|<\/text:p))*?\${container.überschriftabw.versumf}(?:(?<!<\/text:p>).)*?<\/text:p>/is',
$xml,
$matches1,
PREG_OFFSET_CAPTURE
);
<?php
```
Resulted in this output:
```
> $matches1
= [
[
"<text:p
text:style-name="P99"></text:p></table:table-cell><SOME MORE
XML>${/container.überschriftabw.versumf}</text:h><text:p
text:style-name="P71">${container.abw.versumf}</text:p>",
112845,
],
]
```
But I expected this output instead:
```
This part of the regex: (?:.(?!<text:p|<\/text:p))*?
should not match the first passage: <text:p text:style-name="P99"></text:p>
```
To make it work, I remove the < from the mentioned part to get the following regex:
/<text:p[^>\/]*>(?:.(?!<text:p|\/text:p))*?\${container.überschriftabw.versumf}(?:(?<!<\/text:p>).)*?<\/text:p>/is
Then test it against the matched string.
Now the negative lookahead is working again.
The output is [] for $matches1
I tested it with php101.com and didn't experience such weird behaviour.
### PHP Version
8.0.27
### Operating System
_No response_