duanmei1536 2018-07-03 12:05
浏览 28
已采纳

由空格分隔的正则表达式组单词

I have a question concerning Regex and I hope to get some help. I have a bootstrap code containing several expressions like [col md-6 lg-2] or just [col md-6]. However the amount of columns is not fixed and I can have like [col x y z] or even more.

I'd like to achieve a match with groups like: Group 1: x Group 2: y Group 3: z ... and so on.

Some help would be much appreciated

best regards

  • 写回答

1条回答 默认 最新

  • doudang1890 2018-07-03 12:28
    关注

    You'll want to separate your group with preg_replace_callback then split the found string on spaces. Something like this should do it

    $return = '';
    preg_replace_callback('/\[col \K[^\]]+/', function($match){
        $GLOBALS['return'] = preg_split('/\s+/', $match[0], -1, PREG_SPLIT_NO_EMPTY);
    }, '[col md-6 lg-2]');
    print_r($return);
    

    https://3v4l.org/koKFH

    This \[col \K is looking for the literal string [col then the \K tells the regex to forget it matched that for the capturing. The [^\]]+ then captures everything until the next ]. We then use that found group in the callback to separate on whitespaces (\s).

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line