<?
$src='<center>
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> | 
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a>  | 
<a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> | 
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> | 
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> | 
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> | 
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>| 
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> | 
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
              </center>';
$src=preg_replace('/\\|[\\s\\S]*?\\<a href[\\s\\S]*?\\>Exercise\\<\\/a\\>/is','',$src);
echo($src);
?>
输出为<center>
<a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a>  | 
<a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> | 
<a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> | 
<a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> | 
<a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>| 
<a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> | 
<a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
</center>这一行也被去掉了
<a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a>  | 

解决方案 »

  1.   

    $src=preg_replace('/[^\|]*?Exercise<\/a> [\|]?/is','',$src);
    echo $src;
      

  2.   


    $src=preg_replace('#\|[^<]+<a href[^>]+>Exercise</a>#is','',$src);要过滤Exercise所在标签的话,这么写,你的斜杠确实写的太多了
      

  3.   


    $src='<center>
    <a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> | 
    <a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> | 
    <a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a>  | 
    <a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> | 
    <a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> | 
    <a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> | 
    <a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> | 
    <a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>| 
    <a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> | 
    <a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
                  </center>';
    echo($src."\n\n");
    $src=preg_replace('#([|])?[^|<>]+<a href[^>]+>Exercise</a>(?(1)|[^<>]+)#is','',$src);
    echo($src);另外还有这种情况,如果第一个链接是Exercise的时候,前面并没有 | 符号,则是要抹去Exercise链接和“后面的 | 符号”。用条件匹配。
      

  4.   

    确实写多了,我是以为反斜杠有必要再转义一下另外, 我提问的核心是, 为什么我用了非贪婪模式还是匹配多了, 我觉得有必要弄明白附上修改过的正则<?
    $src='<center>
    <a href="/ref-1-k-12-type-GuidedPractice" target="_self">Guided Practice</a> 
    <a href="/ref-1-k-12-type-VideoClip" target="_self">Video Clip</a>
      | 
    <a href="/ref-1-k-12-type-Exercise" target="_self">Exercise</a> | 

    <a href="/ref-1-k-12-type-MathExperiment" target="_self">Experiment</a> | 
    <a href="/ref-1-k-12-type-MathGame" target="_self">Game</a> | 
    <a href="/ref-1-k-12-type-GraphingTool" target="_self">Graphing Tool</a> | 
    <a href="/ref-1-k-12-type-Modeling" target="_self">Modeling</a>| 
    <a href="/ref-1-k-12-type-Demo" target="_self">Demo</a> | 
    <a href="/ref-1-k-12-type-VirtualReality" target="_self">Virtual Reality Media</a>
    </center>';
    $src=preg_replace('/\|[\s\S]*?\<a href[\s\S]*?\>(Exercise)\<\/a\>/is','',$src);
    echo($src);
    ?>
    代码中红色和绿色的都没了,而我只要去掉绿色的
      

  5.   

    楼上的颜色加的有问题, 其实还是原来的 $src
      

  6.   

    那是因为你用了\S,这个通配符代表的意义是一切不为空的字符,配上\s之后就是匹配一切字符了,而你第二行里也是有href的,后面直到下一行Exercise之前都会被匹配,而第一行的href之所以没有被匹配是因为缺少"|"
    非贪婪模式是指匹配到下一个条件生效的最短匹配,这里应该和非贪婪没什么关系,按你写的正则匹配就是最短的了
    你出错是因为\S使得第二行中的href早于第三行的href进行匹配,尽量少使用\S,尽量精确匹配到当前所在行
      

  7.   

    跟据zhang6464所说的,我估计是这样
    $src=preg_replace('/\|[\s\S]*?\<a href[\s\S]*?\>(Exercise)\<\/a\>/is','',$src);
    我的正则,先匹配的|,然后直接匹配到 Exercise </a>
    而我开始以为,有非贪婪的话就可以以 Exercise </a>为基准,在前面匹配尽量少的内容, 而正则看来不是这样的机制