- 2
I can't solve example in regular expressions. Help
3 Respuestas
+ 2
You need to add ^ at starting and $ at end for matching string forward and backward
import re
s=input()
pattern="^[189]\d{7}quot;
if re.match(pattern, s):
print ("Valid")
else:
print ("Invalid")
+ 2
^ is marker for start of string
$ is marker for end of string
in python, 'match' method always search match from string start (marker for start is not required, as it is implicit).
however, 'search' method search from anywhere in the string (so marker for start would be required, if needed).
so, as you're using 'match' method, you could shorter your regular expression as:
pattern="[189]\d{7}quot;
0
))) Oh, it's so easy and really cool! Thanks )))