Python regex problem | SoloLearn: Learn to code for FREE!

+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.

6/2/2017 3:32:38 AM

Deepesh Choudhary

4 Answers

New Answer

+2

in your pattern just add a ".*" before gmailcom so it would give something like this : pattern = r".*gmailcom$" . 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.

+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.

+1

If you replace re.match with re.search it works, but is that what you want?

+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.