Search code examples
pythonpython-re

How to avoid empty strings in pythong's regular expression function re.findAll?


I use python's re module to recognize integers in a sentence. It produces empty strings as well. Any idea on how to remove those empty strings?

In [15]: myre="[0-9]*"

In [16]: re.findall(myre,"23")
Out[16]: ['23', '']

In [17]: re.findall(myre,"23 is a good number.")
Out[17]:
['23',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '']

Solution

  • The reason is your regex matches 0 or more occurences of digits. Change it to [0-9]+ or \d+ and try out.