开发者

regex for getting number after word

开发者 https://www.devze.com 2023-02-06 16:34 出处:网络
i would like to get the error numbers in my error messages. Like Opening and ending tag mismatch: en line 44 and goods

i would like to get the error numbers in my error messages. Like

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

开发者_如何学Go

Premature end of data in tag card line 2

i want to search all that. For that i need a regex like: get me the word (acutally the number) after the word line. its always line. Since i've never worked with regex. Im reading me into it but until now i have had no luck with that.

Im doing that on php. Please give me some input in that. :) thanks


If you want only line numbers, use this:

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

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

If you want to match all line numbers at once:

$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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消