Jump to content

Ask for a simple regexp question!


mengo
 Share

Go to solution Solved by Ch0cu3r,

Recommended Posts

I saw an example when i was learning pcre recently. It is in dot chapter and its address is at http://php.net/manual/en/regexp.reference.dot.php. The codes like this:

Consider,

        preg_match_all("/<img.*>/", $htmlfile, $match);

        preg_match_all("/(*ANY)<img.*>/", $htmlfile, $match);

Now, any character that could possibly be seen as a newline will be interpreted as a newline by the PCRE.

My question is "Does the dot character in pattern preg_match_all("/(*ANY)<img.*>/", $htmlfile, $match) really match newline?" I have thought quite some time but still don't know why. Can anyone help solve this question for me? Thanks!

Link to comment
Share on other sites

i just didn't understand a pattern like this "/(*ANY)<img.*>/", and it seems the use of character sequence "(*ANY)" has never appeared before. What is the meaning of "(*ANY)" here? Or does it should be seen as a pcre option like what the document tells? But when i use a string to match this pattern in the way like this, it seems it can't match $htmlfile:

$htmlfile = "<img  
	     src='1.jpg'/>";
preg_match_all("/(*ANY)<img.*>/", $htmlfile, $matches);
print_r($matches);

How should i understand "(*ANY)" here? It seems pcre doesn't have this kind of use.

Link to comment
Share on other sites

Really thanks Ch0cu3r! I think i have figured out what the document means.

 

In fact, the first pattern sometimes still can match multiple lines if the newline character is "\r", but the default newline character in Windows is "\n", the result is it can matches multiple lines in case "\r" is used but not in case of "\n" .

 

While, the second pattern in case "(*ANY)" is included doesn't match multiple lines again no matter which condition it is, because either "\r" or "\n" will be seen as a newline/linebreak!

 

It seems once in a while there will be one or two places let me headache. So appreciate for your suggestions!

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.