-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 [email protected]

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')
6
  • 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) Commented May 7, 2020 at 5: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. Commented May 7, 2020 at 6:03
  • Sure it does: rextester.com/NBNW46545 Commented May 7, 2020 at 6: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. Commented May 7, 2020 at 6:23
  • You might want to read shinesolutions.com/2018/01/08/… and count how many of the 40 points your regular expression does not cover. Commented May 7, 2020 at 6:26

2 Answers 2

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 [email protected]')))
Sign up to request clarification or add additional context in comments.

3 Comments

Hey Arthur, tried that. Still didn't work. Nothing is printing out.
@Oka6e I edited the code to support upper case letter as well.
I updated the original post with constraints and the original code. I figured out the issue. If the problem didn't break up firstName [email protected] format to a list of the two values, then your code is the answer to the problem.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.