dongru3726 2016-07-20 13:24
浏览 45
已采纳

正则表达式PHP查找字符串并删除父级

in a very big string I have to delete the [w:r][/w:r] where the substring "delete" exist. Example -of substring I want to delete - :

[w:r w:rsidR="00A37EED" w:rsidRPr="00FE1BE1"][w:rPr][w:b][/w:rPr][w:t]delete[/w:t][/w:r]

This one is my best guess \[w:r.*delete.*\[\/w:r\] I tried multiple regex expression but it's not my strong suit.

I copy-pasted the string on regex101 here's the link https://regex101.com/r/wS4bL2/1

I succeeded at finding the required pattern but I can't make it stop at the first occurence of [/w:r].

PHP code -in case you are wondering- :

$this->tempDocumentMainPart = preg_replace('/\[w:r.*delete.*\[\/w:r\]/','',$this->tempDocumentMainPart);
  • 写回答

1条回答 默认 最新

  • douraoyw194498 2016-07-20 13:32
    关注

    The .* will overflow across the [....]s. One way is to use a tempered greedy token:

    \[w:r\b(?:(?!\[w:r\b).)*?delete(?:(?!\[w:r\b).)*?\[\/w:r]
            ^^^^^^^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^
    

    See the regex demo

    The (?:(?!\[w:r\b).)*? tempered greedy token will limit matching inside one [w:r (that has a word boundary on the right).

    Add a DOTALL modifier /s ('/PATTERN/s') so as to match across newlines.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化