I can't solve example in regular expressions. Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

I can't solve example in regular expressions. Help

https://code.sololearn.com/cf6Z04xkEMs6/?ref=app

9th Mar 2021, 12:52 PM
It's me
It's me - avatar
3 Answers
+ 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")
9th Mar 2021, 1:32 PM
Ayush Kumar
Ayush Kumar - avatar
+ 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;
9th Mar 2021, 6:03 PM
visph
visph - avatar
0
))) Oh, it's so easy and really cool! Thanks )))
9th Mar 2021, 2:13 PM
It's me
It's me - avatar