PHP - REGEX - Match Length -


i have regex :

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

to match string :

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

it works, when comes match 1 :

public function privatex ($var,type $typed, $optional = 'option'); 

it fails.

i noticed when length of function's name exceeds 6 chars, not match anymore.

here full 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 pretty simple : why second string not match ?

i can't reproduce in regexbuddy; both declarations match. however, steps needed regex engine arrive @ match double each character. function name of 6 characters takes 100.000 steps of regex engine, 7 characters 200.000 steps, 8 characters 400.000 steps etc.

perhaps regex engine gives after number of steps?

a possessive quantifier (++) cuts down drastically on number of steps needed reducing possible permutations regex engine has go through - 50 steps regardless of length of function name.

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

the reason catastrophic backtracking you're seeing in regex this:

(?:(?:\s*[a-za-z0-9]+)*) 

you nesting quantifiers, , you've made spaces optional. therefore abc can matched abc, a/bc, ab/c or a/b/c. number of permutation rises exponentially every character. further complicate matters making entire group optional (the ? surrounding whole thing).


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -