Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex Newbie
#1
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
Reply
#2
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'
Reply
#3
Thank you very much, it is all clear and now it is working.
Really appreciate the help!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020