【问题标题】:REGEX to get "any_characters((number,number))"正则表达式获取“any_characters((数字,数字))”
【发布时间】:2016-05-26 21:14:29
【问题描述】:

我有这种类型的字符串:

some text that can contains anything ((12.1,5.2))another text that can contains anything ((1,8.7)) and so on...

事实上我不知道如何获得:

[0] 一些可以包含任何内容的文本 ((12.1,5.2))

[1] 另一个可以包含任何内容的文本 ((1,8.7))

...

我试过了:preg_match_all('#(.*\(\([0-9\.]+,[0-9\.]+\)\).*?)#',$lines,$match); 但肯定不行。 我还尝试添加“U”选项和?在 * 和 + 之后,但没有更多结果。

谢谢

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    试试你所拥有的这种变化:

     preg_match_all('#.*?\(\([0-9.]+,[0-9.]+\)\)#',$lines,$match);
    

    开始附近添加的问号很重要,否则点也会消耗左括号。我还删除了右括号之后的部分,假设您的下一行从那里开始。

    你也不需要整个捕获组。

    以下

     if ($match !== false) {
          var_export ($match[0]);
     }
    

    将输出:

    array (
      0 => 'some text that can contains anything ((12.1,5.2))',
      1 => 'another text that can contains anything ((1,8.7))',
    )
    

    【讨论】:

    • 我还有另一个问题,因为我的文本是多行的,我需要添加 's' 选项。谢谢
    【解决方案2】:

    您可以将此正则表达式与preg_match_all 一起使用:

    /.*?\(\([\d.,]+\)\)/
    

    RegEx Demo

    【讨论】:

    • 当括号内的数字超过 2 个时匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2023-03-16
    相关资源
    最近更新 更多