import re
s = "today is 3.6.2015, it is 10:00 o'clock and it's rainy"
mat = re.search(r'(\d{1,2}\.\d{1,2}\.\d{4})', s)
date = mat.group(1)
print date # 3.6.2015
import re
def your_function(num_string):
# Whatever your function does
words_string = "march.third.two thousand fifteen"
return words_string
s = "today is 3.6.2015, it is 10:00 o'clock and it's rainy"
date = re.sub(r'(\d{1,2}\.\d{1,2}\.\d{4})', your_function, s)
print date
# today is march.third.two thousand fifteen, it is 10:00 o'clock and it's rainy
可以使用正则表达式:
注意,如果输入文本中没有与此正则表达式匹配的内容,则会引发一个
AttributeError
,您必须阻止它(例如if mat:
)或处理它。你知道吗编辑
假设可以将转换代码转换为函数,则可以使用
re.sub
:只需修改
your_function
,将3.6.2015
更改为march.third.two thousand fifteen
。你知道吗相关问题 更多 >
编程相关推荐