duanqian6295 2014-04-01 13:08
浏览 20
已采纳

根据字符串中的模式从字符串中删除文本块

I have a big string that is a combination of many blocks (chunks) of data:

{Hello user, good morning \br}{Welcome user, this is just a static text, continue to next page}{Hope you are doing well \br enjoy your stay here.}

What i would like to do is, to remove the chunks from it, that contain a specific character set: \br.

So the above string should be:

{Welcome user, this is just a static text, continue to next page}

I have tried preg_replace, without luck:

$a = "{Hello user, good morning \br}{Welcome user, this is just a static text \br continue to next page}{Hope you are doing well \br enjoy your stay here.}";
$b = preg_replace('/\s+\br\s+/', "", $a);
print $b;
  • 写回答

1条回答 默认 最新

  • doumindang2416 2014-04-01 13:17
    关注

    Try

    $b = preg_replace('/{[^{]*\\br.*?}/', "", $a);

    Breaking that into chunks:

    • { searches for a { character
    • [^{]* means "match any character that isn't a { any number of times (after all, if we find another { it means we're starting a new block. You could also use })
    • ? makes the previous match non-greedy, meaning we'll get the smallest group possible
    • \\br will match \br
    • .*? is a non-greedy match that will match anything that comes before...
    • } the closing brace
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度