【问题标题】:Capture all repeated groups捕获所有重复的组
【发布时间】:2017-01-20 05:18:49
【问题描述】:

我想捕获这个字符串中大括号内的所有字符串:

_{test_1} != '' || _{_str_test_2} != 'Yes' && _{_test_str_3} == 'Yes'

这是我的正则表达式模式:

(?:.*(?:_{(.+)+})+.*)+

但问题是,它只捕获最后一个匹配项。

如何捕获所有匹配项?

谢谢!

【问题讨论】:

    标签: php regex preg-replace preg-match


    【解决方案1】:

    试试这个

    $str = "_{test_1} != '' || _{_str_test_2} != 'Yes' && _{_test_str_3} == 'Yes'";
    $pattern = '#{(.*?)}#s';
    
    preg_match_all($pattern,$str,$matches);
    
    print_r($matches);
    

    【讨论】:

    • 欢迎...! @MonkeyDNaruto
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多