Jump to content

Help pcre subpattern


mengo

Recommended Posts

hello,you all.

 

there is a problem let me not understand.i have a few codes like this:

         $pattern = "/b©(d)\2efgh/";
         $subject = "abcddefghijk";
         preg_match_all($pattern, $subject, $matches);
         echo "<pre>";
         print_r($matches);
         echo "</pre>";

 

i want to use var $pattern capture str "bcddefgh" in var $subject with "\2" back reference,but the result is not what i want,it ouputs nothing but an empty array. i think i did nothing wrong, in php manual, it says a backslash followed by a non-0 number can represent a back reference.but why it fails?

 

is there something wrong with  my codes or an error in php manual? is there some other problems in php.ini or somewhere else?

 

help, thanks!!

 

 

Link to comment
Share on other sites

value of $pattern is not important ,it can also be replaced by "/(b)c(d)\2efgh/",i don't know why  an error occurs when i try to edit the first subpattern,maybe this value is better.


why "\2" can't reference the second subpattern "(d)"?


thanks!


Edited by mengo
Link to comment
Share on other sites

thank you for your help! I found another weird problem,for example:

$pattern = "/(1)(2)(3)(4)(5)(6)(7)((9)\3fgh/";
$subject = "1234567893fghijk";
preg_match_all($pattern, $subject, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
echo chr(2);

form \1 to \7, no matter whick back reference is used, there will always get not correct result! But when changed to \8 or \9, if there is a respondding matching substring in variable $subject,while it will get correct result. Where there is a difference between these two conditions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.