PHP - REGEX - Match Length

I have this regex:

^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*

to match this line:

public function private ($var,Type $typed, $optional = 'option');

This works, but when it comes to this:

public function privateX ($var,Type $typed, $optional = 'option');

Fails.

I noticed that when a function name is longer than 6 characters, it no longer matches.

Here is the complete code:

$strA = 'public function 6Chars ($var,Type $typed, $optional = "option");';
$strB = 'public function MoreThan7 ($var,Type $typed, $optional = "option");';

preg_match('!^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!',$strA,$mA);
preg_match('!^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!',$strB,$mB);

print_r($mA);
print_r($mB);

My question is pretty simple: why doesn't the second line match?

+3
source share
2 answers

I can not reproduce this in RegexBuddy; Both declarations are consistent. However, the steps required by the regular expression engine to achieve a double match with each character. A 6-character function name takes about 100,000 steps of the regular expression engine, 7 characters of 200,000 steps, 8 characters of 400,000 steps, etc.

, regex ?

(++) , , , - 50 .

!^((?:(?:\s*[a-zA-Z0-9]++)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!

, , :

(?:(?:\s*[a-zA-Z0-9]+)*)

, . ABC ABC, A/BC, AB/C A/B/C. . , (?, ).

+3

/multiline /m, , .

gskinner

0

All Articles