-2

Doing a practice problem for Python. Nothing is printing out. I've tried a lot of different things in the pattern variable. pattern = '@gmail\.com' works just fine for the code, but I wanted to be precise with the parameters.

pattern = '^[a-z]{1,20}\s+[a-z]{1,50}@gmail\.com$' works at Rubular.com utilizing the rules shown below on their website. When I do use the above pattern on Python, the syntax error occurs at the carrot or curly brackets. I would like some explanation on what is wrong with the above line of code.

The pattern should be firstName emailID@gmail.com

Constraints

  • First name should be at most 20 characters (edit: and should be lowercase).
  • Email ID is at most 50 characters (edit: and should be lowercase).
  • All emails should be @gmail.com

My Code

import math
import os
import random 
import re
import sys

def appendUserName(firstNameEmailID):
    pattern = '^[a-z]{1,20}\s+[a-z]{1,50}@gmail\.com$'

    if re.search(pattern,emailID):
        userList.append(firstName)

if __name__ == '__main__':
    N = int(input())
    userList = []

    for N_itr in range(N):
        firstNameEmailID = input().split()

        firstName = firstNameEmailID[0]

        emailID = firstNameEmailID[1]

        appendUserName(firstNameEmailID)

    print(*sorted(userList), sep = '\n')
Oka6e
  • 45
  • 7
  • 2
    You shouldn't have `+` after a `{}` quantifier. Try `'^[a-zA-Z]{1,20}\s+[a-zA-Z]{1,50}@gmail\.com$'` (remove the `A-Z` if you're using the `i` flag) – Nick May 07 '20 at 05:44
  • I forgot to add that the other constraint was lowercase letters for email, but that doesn't really matter for the overall error. `i` flag is not being used. Took your advice and removed the 2 respective `+` and code still didn't work. Added my code to the original post. – Oka6e May 07 '20 at 06:03
  • Sure it does: https://rextester.com/NBNW46545 – Nick May 07 '20 at 06:18
  • Apologies, the problem was not with the updated line of code. I figured out it was a problem with other parts of the code. Should have posted the whole code. – Oka6e May 07 '20 at 06:23
  • You might want to read https://shinesolutions.com/2018/01/08/falsehoods-programmers-believe-about-names-with-examples/ and count how many of the 40 points your regular expression does not cover. – Klaus D. May 07 '20 at 06:26
  • Lol, I was simply trying to solve a basic problem from the internet, but I’ll keep that in mind. – Oka6e May 07 '20 at 06:46

2 Answers2

0

You should remove the + sign after the braces {}, also you must add [A-Z] to your alphabetic character matching.

So, your regex should be:

pattern = '^[a-zA-Z]{1,20}\s+[a-zA-Z]{1,50}@gmail.com$'

To have a more complete test code for this example:

import re
pattern = '^[a-zA-Z]{1,20}\s+[a-zA-Z]{1,50}@gmail.com$'
p = re.compile(pattern)
print(bool(p.match('firstName emailID@gmail.com')))
arthursribeiro
  • 326
  • 3
  • 9
  • Hey Arthur, tried that. Still didn't work. Nothing is printing out. – Oka6e May 07 '20 at 06:05
  • @Oka6e I edited the code to support upper case letter as well. – arthursribeiro May 07 '20 at 06:18
  • I updated the original post with constraints and the original code. I figured out the issue. If the problem didn't break up `firstName emailID@gmail.com` format to a list of the two values, then your code is the answer to the problem. – Oka6e May 07 '20 at 06:26
0

First part of the pattern code included the firstName for the email variable. Created an emailPattern and namePattern variable.

def appendUserName(firstNameEmailID):
    emailPattern = '[a-z]{1,50}@gmail\.com'
    namePattern = '[a-z]{1,20}'


    if re.search(emailPattern,emailID) and re.search(namePattern,firstName):
        userList.append(firstName)
Oka6e
  • 45
  • 7