简体   繁体   中英

Problem with multiple matches on same line in simple regular expression

I'm having a very basic question about regular expressions. I am trying to match and replace URLs like these:

http://mydomain.com/image/13/imagetitle.html

For which I use the following expression:

/mydomain.com(.*)image\/(\d+)\/(.*).html/

This pattern works fine mostly, yet it does not work when multiple occurrences appear on the same line. So this works:

This is my own image: http://mydomain.com/image/13/imagetitle.html

When including multiple occurrences across lines it works as well:

This is my own image: http://mydomain.com/image/13/imagetitle.html
Yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html

Both occurrences match and are replaced correctly. However, it only replaces the first match when there are two occurrences on the same line:

This is my own image: http://mydomain.com/image/13/imagetitle.html, yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html

How can I make sure all matches are replaced, regardless of new lines?

I didn't get the problem either. But just judging from the regex, your issue is likely to be the greediness.

(.*) matches as much as it can. It will catch two URLs at once, if they are on the same line. Typically you therefore want to use (.*?) instead, or apply the ungreediness /U flag.

But in your case I'd advise simply making the match more specific:

/mydomain.com(\S*)image\/(\d+)\/(\S*).html/

Here the \\S will only match anything that isn't whitespace, because that's most certainly where URLs should be broken up. As alternative you could use a more specific character class like ([\\w/.?&#%=-]*) instead of .*? there.

Your pattern is working. I had tested it by the foll code:

$data = "This1 is my own image: http://mydomain.com/image/13/imagetitle.html, yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html
This2 is my own image: http://mydomain.com/image/13/imagetitle.html, yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html
This3 is my own image: http://mydomain.com/image/13/imagetitle.html, yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html
This4 is my own image: http://mydomain.com/image/13/imagetitle.html, yet I recommend this one as well: image: http://mydomain.com/image/15/imagetitle2.html
";
echo preg_replace('/mydomain.com(.*)image\/(\d+)\/(.*).html/', 'replaced one', $data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM