dousheng3364 2016-03-17 17:00
浏览 36
已采纳

无法在Php或Notepad ++中按行完成正则表达式替换

190 PALMER RD, HARMONY
MLS® 201512263 $239,900
2 ACRES/HEATED DBL GARAGE/HEAT PUMP
http://goo.gl/tVWDan

I want the above first line '190 PALMER RD, HARMONY' replaced with : <p style="color: #EF2F48"> <b> 190 PALMER RD, HARMONY </b> </p> using regex in PHP.

I used the following code. But doesn't work. Please help.

(^[0-9].*) - <p style="color: #EF2F48"> <b> \1 </b> </p>

I want the above second line 'MLS® 201512263 $239,900' replaced with : <p> MLS® 201512263 &nbsp;&nbsp; $239,900 </p> using regex.

I used the following code. But works.

(MLS®[0-9]*) (\$[0-9,]+) - <p> \1 &nbsp;&nbsp; \2 </p>

And I want the third line select by regex. I am not sure. I want like this. <p style="color: #000"> 2 ACRES/HEATED DBL GARAGE/HEAT PUMP </p>

Fourth line I selected like this way and replaced.

(http:*.*) - <p><a class="ui-link" style="color: blue;"href="\1" target="_blank">\1</a></p>

Please help me.
Manoj

  • 写回答

1条回答 默认 最新

  • dongzaijiao4863 2016-03-17 17:25
    关注

    Try following script:

    $rx = '/^([^
    ]+)\s*([^
    ]+) (\$[^
    ]+)\s*([^
    ]+)\s*([^
    ]+)$/sim';
    $replacement = '<p style="color: #EF2F48"><b>$1</b></p><p>$2&nbsp;&nbsp$3</p> <p style="color: #000">$3</p> <p><a class="ui-link" style="color: blue;"href="$4" target="_blank">$5</a></p>';
    
    $subject = ".... your test data ....";
    
    $result = preg_replace($rx, $replacement, $subject);
    

    Result:

    <p style="color: #EF2F48"><b>190 PALMER RD, HARMONY</b></p>
    <p>MLS® 201512263&nbsp;&nbsp$239,900</p>
    <p style="color: #000">$239,900</p> 
    <p>
    <a class="ui-link" style="color: blue;"href="2 ACRES/HEATED DBL GARAGE/HEAT PUMP" target="_blank">
    http://goo.gl/tVWDan</a>
    </p>
    

    See Demo here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#cesium#的问题:cesium 根据经纬度、高度、方位角、俯仰角和距离求另一点高度
  • ¥15 word脚注设置不起空格符号
  • ¥15 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 求解密json或者美化
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?