正则表达式检查字符串中是否存在数字

use*_*217 1 python regex python-2.7

我有一个清单。我想检查它是否包含一个数字

list1 = [u'Studied at ', u'South City College, Kolkata', u'Class of 2012',
u'Lives in   ', u'Calcutta, India', u'From ', u'Calcutta, India']
>>> if re.match(r'[\w-]+$', str(list1)):
    print "contains a number"
else:
    print "does not contain number"
Run Code Online (Sandbox Code Playgroud)

它不包含任何数字。需要一些帮助。我希望输出为“2012”

ale*_*cxe 5

如何\d正则表达式?

>>> import re
>>> l = [u'Studied at ', u'South City College, Kolkata', u'Class of 2012', u'Lives in ', u'Calcutta, India', u'From ', u'Calcutta, India']
>>> digit_re = re.compile('\d')
>>> [item for item in l if digit_re.search(item)]
[u'Class of 2012']
Run Code Online (Sandbox Code Playgroud)

或者,如果您想提取数字:

>>> for item in l:
...     match = digit_re.search(item)
...     if match:
...         print "%s: %s" % (item, match.group(1))
... 
Class of 2012: 2012
Run Code Online (Sandbox Code Playgroud)