+ 1

re.match python

Hello i have a little problem with the re.match function. import re word = input() if re.match(word, "gl"): print("Match") else: print("No match") this code does not work, it outputs "No match" every time even if the caracters "g" and "l" are in the input string. how can i make it work? thanks for your help this is the question: You are playing a word game with your friends, and are asked for a word starting with "gl". Write a program that takes the word as input and outputs "Match" if it starts with required characters, and "No match" if it doesn't.

4th Jan 2022, 1:08 PM
Ramiz Besic
Ramiz Besic - avatar
3 Respostas
+ 3
Try if re.match("gl",word):
4th Jan 2022, 1:23 PM
Jayakrishna 🇮🇳
+ 2
Look at the documentation again: it is match(substring, string), you passed the arguments the wrong way round. However, you don't need regular expressions for this task. You could simply compare the first 2 characters to "gl"
4th Jan 2022, 1:26 PM
Lisa
Lisa - avatar
+ 1
thanks for your help, it worked!
4th Jan 2022, 1:25 PM
Ramiz Besic
Ramiz Besic - avatar