drt96856 2015-03-19 19:22
浏览 19
已采纳

我的正则表达式没有正确替换

I have this regex: /(?:(?<=(?:style=["])).*)(line-height.*?)[;"]/i

$regex = '/(?:(?<=(?:style=["])).*)(line-height.*?)[;"]/i';

preg_replace("/(?:(?<=(?:style=[\"'])).*)(line-height.*?)[;\"]/i", "HELLO", $input);

This is the input:

    <li><span style="line-height: 20.14399986267089px">500.00dkk</span></li>
<li style="color:red; line-height: 21.14399986267089px"></li>

I want to replace only the occurrences of "line-height: SOMENUMBERpx" with HELLO (It also has to preceded by the style tag): but I can not make it work correctly. Right now it replaces the line-height properties, but it also replace,: color:red, which I do not want.

This is the output I want:

<li><span style=HELLO>500.00dkk</span></li> 
<li style="color:red; HELLO"></li>

Can anyone see what I am doing incorrectly?

  • 写回答

4条回答 默认 最新

  • dtotwai720621 2015-03-23 04:36
    关注

    You can use \K here.\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match

    style=.*?\Kline-height.*?(?=[;"])
    

    Try this.See demo.

    This will make sure that only line=height... will be replced and it is preceded by style= as well

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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制