使用Python正则表达式提取特殊字符串

时间:2015-02-25 20:42:34

标签: regex python-2.7

给出如下字符串:

str = '12-1 abcd fadf adfad'

我想得到12-1。你怎么能在python中做到这一点?

我正在使用以下代码,但不起作用。

m = re.search('.*(\number+-\number+).*', str)
if m:
    found = m.group(0)
    print found

1 个答案:

答案 0 :(得分:2)

尝试:

import re
str = '12-1 abcd fadf adfad'
m   = re.search('(\d+-\d+)', str)
if m:
    found = m.group(0)
    print found
相关问题