假设我有一个这种形式的字符串
this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff
在 Python 中推断数字 72625 的最快方法是什么?
使用re.findall
为您提供最简单的输出,并且适用于任意数量的匹配项。
sent = "this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff"
import re
print re.findall("Time=(\d+)", sent)
# ['72625']
If
>>> st="this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff"
another way of doing it without regex is
>>> st.split("Time=")[-1].split()[0]
'72625'
>>>
import re
input = "this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff"
print re.search('Time=(\d+)', input).group(1)
>>> import re
>>> x = 'this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff'
>>> re.search('(?<=Time=)\d+',x).group()
'72625'
使用正则表达式