Back to Phpstan

regexp.pattern

website/errors/regexp.pattern.md

2.2.11.1 KB
Original Source

Code example

php
<?php declare(strict_types = 1);

$result = preg_match('/[unclosed/', 'test');

Why is it reported?

The regular expression pattern passed to a preg_* function is invalid and would produce a warning or error at runtime. PHP's PCRE engine cannot compile the pattern, which means the function call will fail. PHPStan validates regex patterns at analysis time when they can be statically resolved.

Common causes include missing closing delimiters, unmatched brackets, invalid escape sequences, and other PCRE syntax errors.

How to fix it

Correct the regular expression syntax:

diff-php
 <?php declare(strict_types = 1);

-$result = preg_match('/[unclosed/', 'test');
+$result = preg_match('/\[unclosed/', 'test');

Or if the intent was a character class, close the bracket:

diff-php
 <?php declare(strict_types = 1);

-$result = preg_match('/[unclosed/', 'test');
+$result = preg_match('/[unclosed]/', 'test');

Test the corrected pattern using a tool like regex101 to verify it matches the intended strings before applying the fix.