Skip to content

Commit fac659e

Browse files
committed
Support # comments in regex
1 parent 2d64686 commit fac659e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/Type/Regex/RegexGroupParser.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function count;
2424
use function in_array;
2525
use function is_int;
26+
use function preg_replace;
2627
use function rtrim;
2728
use function sscanf;
2829
use function str_contains;
@@ -64,20 +65,24 @@ public function parseGroups(string $regex): ?array
6465
return null;
6566
}
6667

67-
$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);
68-
try {
69-
$ast = self::$parser->parse($rawRegex);
70-
} catch (Exception) {
71-
return null;
72-
}
73-
7468
$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? '';
7569
foreach (self::NOT_SUPPORTED_MODIFIERS as $notSupportedModifier) {
7670
if (str_contains($modifiers, $notSupportedModifier)) {
7771
return null;
7872
}
7973
}
8074

75+
if (str_contains($modifiers, 'x')) {
76+
$regex = preg_replace('/#.*/', '', $regex);
77+
}
78+
79+
$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);
80+
try {
81+
$ast = self::$parser->parse($rawRegex);
82+
} catch (Exception) {
83+
return null;
84+
}
85+
8186
$captureOnlyNamed = false;
8287
if ($this->phpVersion->supportsPregCaptureOnlyNamedGroups()) {
8388
$captureOnlyNamed = str_contains($modifiers, 'n');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php // lint >= 7.4
2+
3+
namespace Bug12242;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function foo(string $str): void
8+
{
9+
$regexp = '/
10+
# (
11+
([\d,]*)
12+
# )
13+
/x';
14+
if (preg_match($regexp, $str, $match)) {
15+
assertType('array{string, string}', $match);
16+
}
17+
}

0 commit comments

Comments
 (0)