我需要匹配stdout中的几个数字并从字符串中检索它们,字符串可能看起来像这样(多个变体):
"Finished in 1 hour 13 minutes 46 seconds (files took 2.18 seconds to load)"
"Finished in 13 minutes 46 seconds (files took 2.18 seconds to load)"
"Finished in 46 seconds (files took 2.18 seconds to load)"
如何通过一个正则表达式匹配这些数字?我尝试了多种方式,但没有一种能让我得到我需要的东西。
string = "...[one of the above]"
match = string.match(/[different variations of regex]/)
或者我也尝试过:
match = string.scan(/[different variations of regex]/)
我个人不善于正则表达式,任何想法?
预期输出为:
match[0] = 1 hour
match[1] = 13 minutes
match[2] = 46 seconds
感谢@anubhava,我得到了一个解决方案。 解决方案:
match = string.scan(/(?:^|\s)(\d+ (?:hour|seconds|minutes)\b)/)
产生:
[["1 hour"], ["13 minutes"], ["46 seconds"]]
最终实施:
t.scan(/Finished\s+in\s+(?:(?<hour>\d+)\s+hours?\s+)?(?:(?<minute>\d+)\s+minutes?\s+)?(?<second>\d+)\s+seconds?/)
产生:
[["1"], ["13"], ["46"]]
答案 0 :(得分:2)
请改为尝试:
Finished\s+in\s+(?:(?<hour>\d+)\s+hours?\s+)?(?:(?<minute>\d+)\s+minutes?\s+)?(?<second>\d+)\s+seconds?
答案 1 :(得分:1)
您可以使用此正则表达式:
\b\d+(?:\.\d+)? (hour|seconds|minutes)\b
如果您不想匹配浮点值,请使用:
(?:^|\s)(\d+ (?:hour|seconds|minutes)\b)