![]() |
Regex Newbie - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Regex Newbie (/thread-9819.html) |
Regex Newbie - yanhto - Apr-29-2018 Hi, I am beginning with regex in python and i am having trouble with an exercise. It may be very simple and i am probably doing something very wrong but i can t figure out what. I am supposed to check the format of mail adresses: -it has to begin with 1 to 4 letters between a and p -then 0 to 4 number between 0 and 10 -then @gmail.com I am supposed to write a rule that check that, i wrote : '([a-p]{1,4})([0,9]{0,4})([@]{1,1})[g][m][a][i][l][\.][c][o][m]$' It does not work all the time, what am i doing wrong? is there a better way to do it? Thank you very much for your help and understanding RE: Regex Newbie - killerrex - Apr-29-2018 Hi, If you add some of the cases that does not work it will be easy to know the problem but I think it is in the expression to capture the numbers. You are using [0,9] (so, only a 0 or a 9) instead [0-9] (anything between 0 and 9) In any case some general things about that regexp: - Try to use the r'' strings or you will have nightmares with the escape character - Do not create grouping parenthesis if not needed (in your case the @ group). I prefer to match the full username but that depends on the particular case. For complex expressions this is a huge performance difference so using non capturing parentheses (?:) is important (and makes the group output cleaner) - Avoid using character groups of a single letter, makes the things hard to read. Remember that normal text (seasoning with \ as needed) is matched literally. So a reduced version of your expression is: >>> import re >>> m = re.fullmatch(r'([a-p]{1,4}[0-9]{0,4})@gmail\.com$', '[email protected]') >>> m.group(0) '[email protected]' >>> m.group(1) 'lala12' RE: Regex Newbie - yanhto - Apr-29-2018 Thank you very much, it is all clear and now it is working. Really appreciate the help! |