-4

假设我有一个这种形式的字符串

this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff   

在 Python 中推断数字 72625 的最快方法是什么?

4

5 回答 5

10

使用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']
于 2012-04-09T17:34:36.107 回答
3

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'
>>> 
于 2012-04-09T17:42:01.063 回答
2
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)
于 2012-04-09T17:35:54.403 回答
0
>>> import re
>>> x = 'this is a sentence 234225, and some 857307 other stuff, Time=72625, other stuff'
>>> re.search('(?<=Time=)\d+',x).group()
'72625'
于 2012-04-09T17:35:53.993 回答
-1

使用正则表达式

于 2012-04-09T17:33:58.037 回答