Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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
Php 预匹配仅适用于数字_Php_Regex_Preg Match - Fatal编程技术网

Php 预匹配仅适用于数字

Php 预匹配仅适用于数字,php,regex,preg-match,Php,Regex,Preg Match,我试图使用preg_match来提取数字。我试过FLOWING代码,但它不起作用。你知道怎么只知道号码吗 preg_match_all('/^[0-9]+$/i', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result); preg_match_all('/^[0-9]+$/i',这里有一些文本16586个,这里有更多文本',$result); 代码> 你的正则表达式不应该包含 ^ 和

我试图使用preg_match来提取数字。我试过FLOWING代码,但它不起作用。你知道怎么只知道号码吗

preg_match_all('/^[0-9]+$/i', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
preg_match_all('/^[0-9]+$/i',这里有一些文本16586个,这里有更多文本',$result);

代码> 你的正则表达式不应该包含<代码> ^ 和<代码> $符号,因为它们表示字符串的开头和结尾,它不存在于字符串的中间。


取出它们,正则表达式应该可以正常工作。

符号
^
指定行的行距和
$
-行结束。所以你的RegExp完全错了。你应该写

preg_match_all('/\d+/', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
preg_match_all(“/\d+/”,“Jsome text here 16586 more text here.”,$result);

另外,您不需要指定
i
修改器,这意味着使用大小写忽略模式进行搜索。

您已经在正则表达式中使用了
^
$
^
表示匹配字符串的开头,
$
表示匹配字符串的结尾。因此,您编写了一个正则表达式来匹配只包含一个数字的字符串;i、 你的正则表达式说的是字符串的开始,然后是数字,最后是字符串的结束。删除这些字符,您将能够匹配一个数字。

您尝试过吗

preg_match_all('[0-9]+', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
preg_match_all(“[0-9]+”,“Jsome text here 16586 more text here.”,$result);

以下是您应该使用的正则表达式:

/<userinfo>.*?(\d+).*?<\/userinfo>/
/.*(\d+).*/
这将匹配

<userinfo>Jsome text here 16586 more text here.</userinfo>
Jsome text here 16586更多文本here。

返回数组的第二个元素(
array[1]
)将具有所需的编号。(16586)

不要使用。如果字符串包含类似于
“此成本为1.23美元”
的浮点值,您希望发生什么情况?是否要捕获
1
23
?如果不是,您将如何决定数字的开始/结束位置?它将始终是连续的数字否。或者,我必须像这样匹配
\\d+\
谢谢,有什么方法可以让它工作吗<代码>\\d+\
进行了一次var\u转储,它只返回stringWorks中第2个数字的6,速度非常慢!如果内部
只想匹配一个数字,最好使用这一个regexp
'#(?:\D+|(\D+)+#i'
。但是如果你想匹配标签中的所有数字,你应该使用其他的东西