简体   繁体   English

用于 Linux 正常运行时间命令/响应的 PHP preg_match

[英]PHP preg_match for linux uptime command/reponse

so i am using php to decode up time from Linux commands in a text file所以我使用 php 从文本文件中的 Linux 命令解码时间

:~>uptime
:~>23:20pm  up   1:58,  1 user,  load average: 0.11, 0.09, 0.11
:~>

I need 1:58 returned.I have tried:我需要 1:58 返回。我试过:

preg_match('/[0-9]{1,2}:[0-9]{2}/', $a, $b); 

but get results for "23:20".但得到“23:20”的结果。 Additionally "1:58" can grow into a number like "12:34:56" and I need that also.此外,“1:58”可以变成像“12:34:56”这样的数字,我也需要它。

I struggle so much with expressions.我在表达上很挣扎。 I appreciate any help.我很感激任何帮助。

You can try the following regex:您可以尝试以下正则表达式:

/[0-9]{1,2}:[0-9]{2}[:]{0,1}[0-9]{0,2}/

This will check if there is another :00 as part of time, it will get it if not then it will ignore.这将检查是否有另一个:00作为时间的一部分,如果没有,它会得到它然后它会忽略。 Demo演示

Edit: Small problem the above regex will also match something like: 12:3342 .编辑:上面的正则表达式也将匹配类似的小问题: 12:3342 So, for that you can use the following regex( DEMO ):因此,为此您可以使用以下正则表达式( DEMO ):

/[0-9]{1,2}:[0-9]{2}(?(?=[:]{1})([:]{1}[0-9]{0,2})())/

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

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