开发者

PHP RegEx - match to the end of a string

If my string is

Size : 93743 bytes Time elapsed (hh:mm:ss.ms): 00:00:00.156

How do I match the value that appears after the (hh:mm:ss.ms): ?

00:00:00.156

I know how to match if there are more characters following the value, but there aren't any more characters after it and I do not开发者_如何学运维 want to include the size information.


Like so:

<?php
$text = "Size : 93743 bytes Time elapsed (hh:mm:ss.ms): 00:00:00.156";

# match literal '(' followed by 'hh:mm:ss.ms' followed by literal ')'
# then ':' then zero or more whitespace characters ('\s')
# then, capture one or more characters in the group 0-9, '.', and ':'
# finally, eat zero or more whitespace characters and an end of line ('$')
if (preg_match('/\(hh:mm:ss.ms\):\s*([0-9.:]+)\s*$/', $text, $matches)) {
    echo "captured: {$matches[1]}\n";
}
?>

This gives:

captured: 00:00:00.156


$ anchors the regex to the end of the string:

<?php
$str = "Size : 93743 bytes Time elapsed (hh:mm:ss.ms): 00:00:00.156";

$matches = array();

if (preg_match('/\d\d:\d\d:\d\d\.\d\d\d$/', $str, $matches)) {
  var_dump($matches[0]);
}

Output:

string(12) "00:00:00.156"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜