why it doesnt match next | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why it doesnt match next

in this example: import re pattern = r"^gr.y

quot; if re.match(pattern, "grey"): print("Match 1") if re.match(pattern, "gray"): print("Match 2") if re.match(pattern, "stingray"): print("Match 3") if re.match(pattern, "grstingray"): if the string starts with gr. Its the third "if sentence"" why it doesn't match with the regular expression : r"^gr.y
quot; ?

13th Jul 2017, 11:12 AM
Angel Mauricio Antolin Rivas
Angel Mauricio Antolin Rivas - avatar
6 Answers
+ 1
@Angel, can you post your code please? I don't understand your question, sorry xD
13th Jul 2017, 10:48 AM
Gioele Raiano
+ 1
Your RegEx means : first character : g second character : r third character : anything fourth and last character : y So stingray has more than 4 character like grstingray
13th Jul 2017, 11:19 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Baptiste E. Prunier. Thank you so much. I undertood that . could be any number of characters but its just one,
13th Jul 2017, 11:25 AM
Angel Mauricio Antolin Rivas
Angel Mauricio Antolin Rivas - avatar
0
Yes that's it ! You're welcome :)
13th Jul 2017, 11:27 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
so import re pattern = r"^gr..y
quot; if re.match(pattern, "greey"): print("Match 1") if re.match(pattern, "gray"): print("Match 2") if re.match(pattern, "gramystingray"): print("Match 3") if re.match(pattern, "gramy"): print("Match 4") result: Match 1 Match 4
13th Jul 2017, 11:28 AM
Angel Mauricio Antolin Rivas
Angel Mauricio Antolin Rivas - avatar
0
Yes because it needs exactly 2 characters between r and y
13th Jul 2017, 12:34 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar