正则表达式获得数字

xot*_*tix 1 php regex

我想在错误消息中获取错误号.喜欢

打开和结束标签不匹配:第44行和货物

打开和结束标签不匹配:描述第40行和类别

打开和结束标记不匹配:categorieInfo第28行和卡片

标签类别行27中数据的过早结束

标签卡行2中的数据提前结束

我想搜索所有这些.为此我需要一个正则表达式:在字线后面给我一个字(实际上是数字).它总是线.因为我从未使用过正则表达式.我正在读它,但直到现在我还没有运气.

我在php上这样做.请给我一些意见.:) 谢谢

Arn*_*anc 5

如果您只想要行号,请使用:

$msg = 'Opening and ending tag mismatch: en line 44 and goods';

if (preg_match('#\bline (\d+)#', $msg, $matches)) {
    echo "line is: " . $matches[0] . "\n";
}
Run Code Online (Sandbox Code Playgroud)

如果您想一次匹配所有行号:

$msgs = <<<EOF
If you want to match all lines in all messages at once:

Opening and ending tag mismatch: en line 44 and goods

Opening and ending tag mismatch: describtion line 40 and categorie

Opening and ending tag mismatch: categorieInfo line 28 and card

Premature end of data in tag categorie line 27

Premature end of data in tag card line 2
EOF;

preg_match_all('#^.*\bline (\d+).*$#m', $msgs, $matches, PREG_SET_ORDER);
foreach($matches as $msg) {
    echo "message: " . $msg[0] . "\n";
    echo "line: " . $msg[1] . "\n";
}
Run Code Online (Sandbox Code Playgroud)