I'm lost on how the out put doesn't include match 3 yet "g" is part of [a-z] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I'm lost on how the out put doesn't include match 3 yet "g" is part of [a-z]

import re pattern=r"g" if re.match(pattern,"gggg") print("match1") if re.match(pattern,"g") print("match2") if re.match(pattern,[a-z]) print("match3")

5th Nov 2020, 10:55 AM
Moshao Makhele
Moshao Makhele - avatar
6 Answers
+ 2
I am not very good with this topic. This is just an example. import re pattern = r"[a-z]" if re.search(pattern,"g"): print("yes")
5th Nov 2020, 11:23 AM
Avinesh
Avinesh - avatar
+ 6
To explain further what Benjamin Jürgens has said, the format for re.match (and re.search) methods is re.match(regex_pattern, string). So re.match(“[a-z]”, “g”) will return True, but re.match(“g”, “[a-z]”) will return False. The second one is looking for the char “g” at the start of the string “[a-z]”, which is clearly not there.
5th Nov 2020, 12:13 PM
Russ
Russ - avatar
+ 3
"g" is not in "[a-z]" But "g" is in "abcdefgh..." You have to write out the alphabet, you can use regex syntax only for pattern
5th Nov 2020, 11:45 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
It is clearly mentioned in the course. "the re.match function can be used to determine whether it matches at the beginning of a string."
5th Nov 2020, 11:09 AM
Avinesh
Avinesh - avatar
+ 1
Thank you so much
5th Nov 2020, 11:27 AM
Moshao Makhele
Moshao Makhele - avatar
0
Ohhhhh...so in a code like that what would I use to prove that g is in [a-z]
5th Nov 2020, 11:13 AM
Moshao Makhele
Moshao Makhele - avatar