简单正则表达式中同一行上的多个匹配问题

时间:2011-05-21 12:17:26

标签: php regex string

我有一个关于正则表达式的非常基本的问题。我正在尝试匹配并替换以下网址:

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

我使用以下表达式:

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

此模式主要工作正常,但当多次出现在同一行时它不起作用。所以这有效:

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

当跨行包含多个匹配项时,它也可以正常工作:

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

两次出现都匹配并被正确替换。但是,它只会在同一行上出现两次时替换第一个匹配项:

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

如何更换所有匹配项,无论新行如何?

2 个答案:

答案 0 :(得分:2)

我也没有遇到问题。但只要从正则表达式判断,你的问题可能就是贪婪。

(.*)尽可能匹配。如果它们位于同一行,它将同时捕获两个URL。因此,您通常希望使用(.*?),或者应用不明确的/U标记。

但在你的情况下,我建议只是让比赛更具体:

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

这里\S只会匹配任何不是空白的内容,因为这绝对是应该分解URL的地方。作为替代方案,您可以使用更具体的字符类,例如([\w/.?&#%=-]*)而不是.*?

答案 1 :(得分:0)

你的模式正在运作。我用foll代码测试了它:

$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);