dongxifu5009 2015-04-20 09:57
浏览 32

仅在<code> </ code>之间匹配所有选项卡

Sample Text

    Hello
    World

<pre><code>&lt;VirtualHost *:80&gt;
    ServerAdmin abc@gmail.com
    DocumentRoot &quot;C:\wamp\www\abc\public&quot;
    ServerName abc.local
    ErrorLog &quot;logs/abc.local-error.log&quot;
    CustomLog &quot;logs/abc.local-access.log&quot; common
&lt;/VirtualHost&gt;
</code></pre>

<pre><code>&lt;VirtualHost *:80&gt;
    ServerAdmin abc@gmail.com
    DocumentRoot &quot;C:\wamp\www\abc\public&quot;
    ServerName abc.local
    ErrorLog &quot;logs/abc.local-error.log&quot;
    CustomLog &quot;logs/abc.local-access.log&quot; common
&lt;/VirtualHost&gt;
</code></pre>

    Hello
    World

I would like to match all tabs only between <code></code>, I have tried using /(<code>)*([\t])+/g, but it still matches all tabs (everywhere), I intend to replace those tabs with 2 spaces later

Please guide or hint me with some regex, thanks

  • 写回答

1条回答 默认 最新

  • duancao1951 2015-04-20 10:28
    关注

    Try this, First match anything between code

    $txt='<code>test</code>';
    
      $re1='(<code>)';  # Tag 1
      $re2='.*?';   # Non-greedy match on filler
      $re3='(<\\/code>)';   # Tag 2
    
      if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $txt, $matches))
      {
          $tag1=$matches[1][0];
          $tag2=$matches[2][0];
          print "($tag1) ($tag2) 
    ";
      }
    

    Then on this result, use your reg expression

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大