-3

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',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '']
1

2 Answers 2

3

You should be using [0-9]+ rather than [0-9]*; the latter matches against 0 or more (meaning that the digits are optional); +, however, matches against one or more.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.