Link to non-existent subpattern
I made some progress with my regex I use to extract attributes from pseudo xml tags, but then I got ambition and wanted to handle quoted attributes correctly (with quotes optional):
regular expression
~\{language\s*=\s*(P?<quote>[\"\']*)(?P<att>.*?)(?P=quote)\s*/\}~
(this is the result of var, which is used as arg in preg_match, so "sane things" like \ were created with chr(92) . chr(34)
beforehand ...)
entrance
kjkjkjkjkjkj{language= 'DE' /}xxxxlxlxlxlllllk
retrieves 'DE
' when tested with RegexBuddy. But PHP preg_match throws a warning: Warning: preg_match (): Compile error: reference to non-existent subpattern at offset 56.
What's the problem? I thought the "quote" was assigned earlier ...
Here's the complete program , just in case I have a PHP error:
<?php
$QQ=chr(92) . chr(34);
$delimeters = "{}";
$del0 = preg_quote($delimeters{0});
$del1 = preg_quote($delimeters{1});
$tag="language";
$string="fdfdfdfdf{language=1}testhgg";
$preg1 = "|" . $del0 . $tag . "[^" . $del1 . "]*" . $del1 . "(.*?)" . $del0 . "/" . $tag . $del1 . "|";
$preg2 = "~" . $del0 . $tag . "\s*=\s*(?P<" . "quote>[" . $QQ . "\']*)(?P<att>.*?)(?P=quote)\s*/" . $del1 . "~";
$match=array();
preg_match($preg1,$string,$match);
echo "<br>match1:<pre>";var_dump($match);echo"</pre>";
$match=array();
preg_match($preg2,$string,$match);
echo "<br>match2:<pre>";var_dump($match);echo"</pre>";
?>
source to share
Your subheading is not formatted correctly.
(P?<quote>[\"\']*)
it should be
(?P<quote>[\"\']*)
See http://php.net/manual/en/regexp.reference.subpatterns.php
source to share