doubi4435 2018-04-17 19:51
浏览 54
已采纳

字符串缺失分隔符上的单个捕获组的PCRE正则表达式

I have html with this sort of node value:

80% Polyester 10% Cotton 10% Rayon

It can also be like these:

90% Cotton 10% Rayon

Or:

100% Cotton

EDIT -- ALSO the text to capture might be more than one word like:

90% Egyptian Cotton 10% Polyester

What I need to do in PHP is change it to be like:

<li>80% Polyester</li>
<li>10% Cotton</li>
<li>10% Rayon</li>

I'm not sure how to get the capture group quite right... maybe there needs to be two capture groups that can happen 1+ times?

preg_match('/<p>([1-9]?[0-9]?% [^\d<]+|^100% )+<\/p>/', '<p>80% Polyester 10% Cotton 10% Rayon</p>', $matches);

This only finds 1 match for: 10% Rayon... what am I doing wrong for it not to capture all 3 groups?

EDIT: PLEASE FORGET ABOUT THE HTML TAGS

I stripped the tags from the question because people can't get over the whole regex and html issue... think of this as just a data string please.

  • 写回答

3条回答 默认 最新

  • dtsnx44260 2018-04-17 20:09
    关注

    You may use this preg_replace:

    $repl = preg_replace('/([\d.]+%\h*\pL+(?:\h+\pL+)*)\h*/', 
                         '<li>$1</li>
    ', $str);
    

    RegEx Demo

    ([\d.]+%\h*\pL+(?:\h+\pL+)*)\h* matches and groups 1+ digits followed by % followed by 0+ spaces followed by 1+ letter and space characters.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分