I am working on regular expression python, I came across this problem.
A valid mobile number is a ten digit number starting with a 7,8 or 9. my solution to this was :
if len(x)==10 and re.search(r'^[7|8|9]+[\d+]$',x):
for which i was getting error. later I changed it to
if len(x)==10 and re.search(r'^[7|8|9]+\d+$',x):
for which all test cases passed. I want to know what the difference between using and not using []
for \d+
in regex ?
Thanks
[\d]+
would have been correct,[\d+]
means one charatcer thats either a number or a plus. Also remove the|
s from the character class, as they are treated as just literal|
[]
where you really want()
.[]
looks for a single character inside. Eg:[abc]
look fora
,b
orc
.