【问题标题】:Preg Match only digits Between Curly Braces - preg_matchPreg 仅匹配大括号之间的数字 - preg_match
【发布时间】:2018-04-05 12:04:53
【问题描述】:

Preg Match Between Curly Braces - preg_match相关

如果我的字符串是:这是一个包含 {2} 个单词和 {7} 个数字的示例

我想回来:

array(
[1] => 2
[2] => 7
)

【问题讨论】:

    标签: find match str-replace digits curly-braces


    【解决方案1】:

    如果链接的问题使用. 匹配任何 字符,您可以使用\d 匹配任何数字字符(0-9):

    $str = 'this an example with {2} words and {7} numbers';
    
    if(preg_match_all('/{+(\d*?)}/', $str, $matches)) {
        var_dump($matches[1]);
    }
    

    输出:

    array(2) {
      [0]=>
      string(1) "2"
      [1]=>
      string(1) "7"
    }
    

    https://regex101.com/r/MmXp6E/1

    【讨论】:

    • 谢谢你,维达利亚!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多