gliese581g
gliese581g

Reputation: 443

Python regular expression match not working

I have written below given code to match a string of fixed length of 10 which contains all digits.

import re
result=re.match("^d{10}$", u"5478512045")

But it returns None. I don't know why is it failing. Please correct me if I am doing anything wrong over here.

Upvotes: 0

Views: 1660

Answers (1)

Mark Streatfield
Mark Streatfield

Reputation: 3209

You are missing an escape \ on the d control character. It should be:

result=re.match("^\d{10}$", u"5478512045")

Without the \ before the d, your regex is trying to match a literal d string. By changing this to \d you match against the special character for any decimal digit.

Upvotes: 1

Related Questions