php - 正则表达式获取 "any_characters((number,number))"

标签 php regex preg-match-all

我有这种类型的字符串:

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

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

[0] some text that can contains anything ((12.1,5.2))

[1] another text that can contains anything ((1,8.7))

...

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

谢谢

最佳答案

尝试使用您所拥有的这种变体:

 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))',
)

关于php - 正则表达式获取 "any_characters((number,number))",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418208/

相关文章:

java - 如果使用空格作为正则表达式的分隔符,如何处理值中的空格?

Python就地配置值更新

javascript - 正则表达式匹配 Markdown 链接

php - 顺序和选择子句中的 Full Group BY 错误

PHP 分页 有限数量的记录,但不是全部

php - php 中的文本框自动填充

php - 大写单词的正则表达式

php - preg_match_all 不使用 mysql 数据库结果变量?

php - 如何在 PHP 中匹配具有公共(public)前缀的单词?

php - 复杂的mysql查询创建html表