dsms21398 2013-01-09 15:00
浏览 140
已采纳

PHP正则表达式:匹配最接近的一个

I have a string like this

<div><span style="">toto</span> some character <span>toto2</span></div>

My regex:

/(<span .*>)(.*)(<\/span>)/

I used preg_match and it returns the entire string

<span style="">toto</span> some character <span>toto2</span>

I want it returns:

<span style="">toto</span>
and
<span>toto2</span>

What do I need to do to achieve this? Thanks.

  • 写回答

2条回答 默认 最新

  • dongpan1416 2013-01-09 15:07
    关注

    How about this:

    /(<span[^>]*>)(.*?)(<\/span>)/
    

    Check the docs here at PHP preg_match Repetition:

    By default, the quantifiers are "greedy", that is, they match as much as possible

    and

    However, if a quantifier is followed by a question mark, then it becomes lazy, and instead matches the minimum number of times possible

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器