Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 更正我的预匹配函数_Regex_Preg Match - Fatal编程技术网

Regex 更正我的预匹配函数

Regex 更正我的预匹配函数,regex,preg-match,Regex,Preg Match,我有一些数据要从中获取值: Mobile:7899886788 bal:0.0 p=1898668 我已为此设置了一个preg\u match函数: preg_match('#Mobile:(.*?) bal:0.0 p=(.*?)#',$html,$matches); $a=matches[1]; $a1=$matches[2]; 我正在获取$a的值,但找不到$a1的值。请告诉我我的解决方案有什么问题。由于*?量词是惰性的,因此正则表达式中的第二组总是匹配空字符串 如果在

我有一些数据要从中获取值:

 Mobile:7899886788  bal:0.0    p=1898668
我已为此设置了一个
preg\u match
函数:

preg_match('#Mobile:(.*?)  bal:0.0    p=(.*?)#',$html,$matches);
$a=matches[1];
$a1=$matches[2];

我正在获取
$a
的值,但找不到
$a1
的值。请告诉我我的解决方案有什么问题。

由于
*?
量词是惰性的,因此正则表达式中的第二组总是匹配空字符串

如果在
Mobile:
p=
之后始终有数字值,则可以用简单的数字匹配模式替换所有的点惰性匹配模式
\d+

'#Mobile:(\d+)\s+bal:0\.0\s+p=(\d+)#'

注意点必须转义以匹配文字点。此外,最好使用与任何一个或多个空白字符匹配的
\s+
模式替换文字空格

另一种方法是使用
\S+
捕获所需属性后的任何1+非空白符号:

'#Mobile:(\S+)\s+bal:\S+\s+p=(\S+)#'

请参见

由于*?量词是惰性的,因此正则表达式中的第二组总是匹配空字符串

如果在
Mobile:
p=
之后始终有数字值,则可以用简单的数字匹配模式替换所有的点惰性匹配模式
\d+

'#Mobile:(\d+)\s+bal:0\.0\s+p=(\d+)#'

注意点必须转义以匹配文字点。此外,最好使用与任何一个或多个空白字符匹配的
\s+
模式替换文字空格

另一种方法是使用
\S+
捕获所需属性后的任何1+非空白符号:

'#Mobile:(\S+)\s+bal:\S+\s+p=(\S+)#'