简体   繁体   English

preg_match_all得到最后一场比赛

[英]preg_match_all get last match

Is it possible to get the last match using preg_match_all ? 是否可以使用preg_match_all获取最后一场比赛? Using this regex will only return 126 81 and that's not what I'm after. 使用这个正则表达式只会返回126 81,这不是我所追求的。 In this case I want 183 34 to be returned. 在这种情况下,我想要返回183 34。

$string = 'Korpstigen 126 183 34 Kalmar';
preg_match_all("/[0-9]{5}|[0-9]{3} [0-9]{2}/", $string, $matches, PREG_OFFSET_CAPTURE);

I suggest using word boundaries \\b : 我建议使用单词边界\\b

preg_match_all("/\b[0-9]{5}\b|\b[0-9]{3} [0-9]{2}\b/", $string, $matches, PREG_OFFSET_CAPTURE);

regex101 demo . regex101演示

If you're looking to just get 183 34 and ignore all other parts of the string: 如果你想获得183 34并忽略字符串的所有其他部分:

preg_match_all("/[0-9]{3} [0-9]{2} /", $string, $matches);

will return 将返回

Array
(
    [0] => Array
        (
            [0] => 183 34 
        )

)

I'm not sure of your needs, but if you need all three numbers 我不确定你的需求,但如果你需要这三个数字

preg_match_all("/[0-9]{2,3}/", $string, $matches);

will return 将返回

Array
(
    [0] => Array
        (
            [0] => 126
            [1] => 183
            [2] => 34
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM