Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression to match an integer as string [duplicate]

Tags:

python

regex

I am trying to filter a list of strings with regular expressions, as shown in this answer. However the code gives an unexpected result:

In [123]: r = re.compile('[0-9]*')
In [124]: string_list = ['123', 'a', '467','a2_2','322','21']
In [125]: filter(r.match, string_list)
Out[125]: ['123', 'a', '467', 'a2_2', '322_2', '21']

I expected the output to be ['123', '467', '21'].

like image 412
Mike Vella Avatar asked Dec 22 '13 21:12

Mike Vella


1 Answers

The problem is that your pattern contains the *, quantifier, will match zero or more digits. So even if the string doesn't contain a digit at all, it will match the pattern. Furthermore, your pattern will match digits wherever they occur in the input string, meaning, a2 is still a valid match because it contains a digit.

Try using this pattern

^[0-9]+$

Or more simply:

^\d+$

This will match one or more digits. The start (^) and end ($) anchors ensure that no other characters will be allowed within the string.

like image 194
p.s.w.g Avatar answered Sep 24 '22 20:09

p.s.w.g