正则表达式

时间:2013-10-11 12:31:24

标签: php regex preg-replace

我们想使用preg_replace进行查找和替换。但无法获得理想的结果

这是我的字符串

    $x = '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/10/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/20>';05/1/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/9/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2006/11/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
    $x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/i_leave_shreds_.html#comment-11657412">FALLACI</a

    echo preg_replace('/<a(.*?)href="http:\/\/atlasshrugs2000.typepad.com\/atlas_shrugs\/([0-9\/]{0,7}?)(.*?)_.html#(.*?)"(.*?)>/','<a$1href="http://localhost/test/$3#$4"$5>',$x);

它给出了以下结果

<a href="http://localhost/test/2005/11/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/2005/10/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/2005/1/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/2005/9/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/2006/11/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>

但我们想要像

这样的结果
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>
<a href="http://localhost/test/i_leave_shreds#comment-11657412">FALLACI</a>

请帮帮我。 在此先感谢:)

3 个答案:

答案 0 :(得分:2)

解决方案

如果我们从你当前的正则表达式模式开始...

此:

$x = '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/10/i_leave_shreds_.html#comment-11657411">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/1/i_leave_shreds_.html#comment-11657412">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/9/i_leave_shreds_.html#comment-11657413">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2006/11/i_leave_shreds_.html#comment-11657414">FALLACI</a>';
$x .= '<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/i_leave_shreds_.html#comment-11657415">FALLACI</a>';

echo preg_replace('~<a.*?href=["\'].*?/([^/]*?)_\.html#(.*?)["\'].*?>(.*?)</a>~', "<a href='http://localhost/test/$1#$2'>$3</a><br>\n", $x);

输出:

<a href='http://localhost/test/i_leave_shreds#comment-11657410'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657411'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657412'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657413'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657414'>FALLACI</a><br>
<a href='http://localhost/test/i_leave_shreds#comment-11657415'>FALLACI</a><br>

正则表达式解释

~<a.*?href=["'].*?/([^/]*?)_\.html#(.*?)["'].*?>(.*?)</a>~
  • ~ =启动分隔符
  • <a.*? =匹配开头a标记后跟任意字符0次或更多次,直到达到...
  • href=["'] =匹配href=后跟"'
  • .*?/ =匹配所有字符,直到最后的斜杠...
  • ([^/]*?) =捕获组并捕获最终斜杠和...之间的所有内容。
  • _\.html# =匹配网址的下划线和html文件扩展名,后跟#
  • (.*?) =捕获组匹配所有字符(注释/数字)...
  • ["'].*?> =匹配"',然后匹配任何字符数0次或更多次,直到它到达开始a标记的末尾:>
  • (.*?) =匹配开始和结束a代码之间的文字:FALLACI
  • </a> =匹配结束a代码

更新

要将替换限制为仅包含:atlasshrugs2000.typepad.com的替换,您可以将正则表达式更新为:

~<a.*?href=["\'].*?atlasshrugs2000.typepad.com.*?/([^/]*?)_\.html#(.*?)["\'].*?>(.*?)</a>~

此正则表达式与原始正则之间的区别在于(上面的项目符号列表第4行):

.*?/                                <-- Original
.*?atlasshrugs2000.typepad.com.*?/  <-- Updated

只需更新版本检查特定网址http://之前的任何字符(例如atlasshrugs2000.typepad.com),然后检查其后的任何字符。

匹配示例(http / https / BLANK):

<a href="http://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>
<a href="atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>
<a href="https://atlasshrugs2000.typepad.com/atlas_shrugs/2005/11/i_leave_shreds_.html#comment-11657410">FALLACI</a>

答案 1 :(得分:0)

问题在于:([0-9\/]{0,7}?) ...您已经拥有0-7个实例,然后您希望获得尽可能少的实例。你不需要同时指定......删除?最后(所以它看起来像([0-9\/]{0,7}))然后它会工作。

答案 2 :(得分:0)

尝试:
/<a(.*?)href="http:\/\/atlasshrugs2000.typepad.com\/atlas_shrugs\/([0-9\/]{0,7})\/(.*?)_.html#(.*?)"(.*?)>/

{0,7}?)(更改为{0,7})\/(