I want to get the first match of a regex.
In this case, I got a list:
text = \'aa33bbb44\'
re.findall(\'\\d+\',text)
You could embed the ''
default in your regex by adding |$
:
>>> re.findall('\d+|$', 'aa33bbb44')[0]
'33'
>>> re.findall('\d+|$', 'aazzzbbb')[0]
''
>>> re.findall('\d+|$', '')[0]
''
Also works with re.search
pointed out by others:
>>> re.search('\d+|$', 'aa33bbb44').group()
'33'
>>> re.search('\d+|$', 'aazzzbbb').group()
''
>>> re.search('\d+|$', '').group()
''