+ 3

Python regex problem

Below code should check if the string ends with "gmailcom". The code seems correct to me but it's not showing expected output. https://code.sololearn.com/cZdBedXx37RO/?ref=app Please tell the problem and correct me.

2nd Jun 2017, 3:32 AM
Deepesh Choudhary
Deepesh Choudhary - avatar
4 odpowiedzi
+ 2
in your pattern just add a ".*" before gmailcom so it would give something like this : pattern = r".*gmailcom
quot; . is what is called a meta character and it stands for any symbol except new lines and some other special things... the *symbol means that there might ne any number of . character in the string. (you could have used + instead of * but i would have mean at least one "."). In reality the regex match verify the match from the beginning of a string that's why you have tp specify what is there at the begining of your string. anothet solution is to use the search function because the $ symbol is gonna say that you only check the pattern at the end of the string.
2nd Jun 2017, 5:10 AM
Glozi30
Glozi30 - avatar
+ 11
I'd double check with the regex definitions (I don't remember the details offhand): Do you need a wildcard, or other pattern, to allow for the early part of the string? Sorry I can't be more specific, but there are different matching conventions, and the details are eluding me right now.
2nd Jun 2017, 4:23 AM
Jim
Jim - avatar
+ 1
If you replace re.match with re.search it works, but is that what you want?
2nd Jun 2017, 4:46 AM
Paul
Paul - avatar
+ 1
@Paul @Glozi30 I had just read about python metacharacters and wanted to try something with $. I forgot that I also had to write something about the beginning(if using "match"), as it was in the tutorial. Thanks for defining the starting of expression, and I used match just because the tutorial did it without problem, and my code caused a problem, I can also use search.
2nd Jun 2017, 6:01 AM
Deepesh Choudhary
Deepesh Choudhary - avatar