dongqie2010 2013-11-08 10:21
浏览 54
已采纳

PHP正则表达式替换两个表达式中的表达式

I'm trying to create a Regex expression to use with preg_replace() to replace a expression that's between two other expressions. The overall idea is:

$new = "stuff";
$original = /*see below*/;
$final = preg_replace('regex expression', $new, $original);

example of $original string:

(...)"dl cm = xxxx opop1 = XYZ opop3 = XXXXXXXXXXXX,XXXX,"thisisit" YYYY = z"(...)

example of the $final string:

(...)"dl cm = xxxx opop1 = XYZ opop3 = XXXXXXXXXXXX,XXXX,"stuff" YYYY = z"(...)

(The X's, Y's, Z's can be any character)

The expression must find the 'opop3 = XXXXXXXXXXXX,XXXX,"' then the following '"' and change the text inside this two delimiters.

I think that I found how to find the first delimiter

(opop3.=(.*)(\,)(.*)(\,)\")

And find the following '"' is not hard, but i don't know how can I change the text between them without touching the delimiters.

(or if someone know other way to do this, please, let me know)

  • 写回答

1条回答 默认 最新

  • duanpa2143 2013-11-08 10:27
    关注

    You can make use of \K (reset search) of PCRE:

    $final = preg_replace('/XXXXXXXXXXXX,XXXX,"\K[^"]+/', $new, $original);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!