+ 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.
3 Réponses
+ 3
Try
if re.match("gl",word):
+ 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"
+ 1
thanks for your help, it worked!