正则表达式得到 "any_characters((number,number))"
REGEX to get "any_characters((number,number))"
我有这种类型的字符串:
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
:
/.*?\(\([\d.,]+\)\)/
尝试使用您现有的变体:
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))',
)
我有这种类型的字符串:
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
:
/.*?\(\([\d.,]+\)\)/
尝试使用您现有的变体:
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))',
)