php - PCRE regular expressions using named pattern subroutines -


i experimenting named subpattern/'subroutine' regex features in php's pcre , i'm hoping can explain following strange output:

$re = "/ (?(define)     (?<a> ) )  ^(?&a)$  /x";  var_dump(preg_match($re, 'a', $match)); // (int) 1 expected var_dump($match); // array( [0] => 'a' ) <-- why? 

i can't understand why named group "a" not in result (with contents "a"). changing preg_match preg_match_all puts "a" , "1" in match data both contain empty string.

i idea of writing regular expressions way, can make them incredibly powerful whilst keeping them maintainable (see this answer example of this), if subpatterns not available in match data it's not use really.

am missing here or should mourn have been , move on?

it makes perfect sense these subpatterns not capture group - main purpose used more once, can't capture them all. in addition, if default capture subpatterns wouldn't give option not capture group don't want - not best default behavior. opposite trivial - can capture adding group around (?&a) statement.
couldn't find reference on pcre.org. closest this, relevant because don't match (?<a>...) directly (though might expect empty group):

any capturing parentheses set during subroutine call revert previous values afterwards.

it clearer on perl manual (relevant part highlighted):

an example of how might used follows:

/(?<name>(?&name_pat))(?<addr>(?&address_pat)) (?(define) (?<name_pat>....) (?<adress_pat>....) )/x 

note capture buffers matched inside of recursion not accessible after recursion returns, layer of capturing buffers necessary.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -