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 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'