在php中返回数字的正则表达式


Regular expression to return a number in php

嗨,我有一个字符串,想搜索匹配的字符串,例如

[table id=345/]

并希望它返回数字345。我可以知道规则是什么吗?

我的规则是:

preg_match("/'[table id=([^]*?) '/']/s", $char, $match);

但不工作。

如果有多个匹配模式,如何获得它返回?目前,它只返回第一个事件。

试试这个:

preg_match("@'[table ([0-9]+) '/']@s",$char,$match);

这是你的正则表达式:

'[table id='K'd+

这个示例代码打印所有匹配项(参见在线演示底部的输出):

$string = "[table id=345 /] [table id=123 /]
[table id=999 /] [table id=000 /]";
$regex = "~'[table id='K'd+~";
$count = preg_match_all($regex,$string,$m);
print_r($m[0]);

解释Regex

'[                       # '['
table id=                # 'table id='
'K                       # Keep what has been matched so far out of the returned match
'd+                      # digits (0-9) (1 or more times (matching
                         # the most amount possible))