Home > Software design >  Non greedy regex pattern for email Python
Non greedy regex pattern for email Python

Time:12-29

I m new to regular expression and stuck at this situation where my current regex pattern is returning two results and one of them starting with the phone no. (String doesn't have a space between phone no and email). I tried using the ? to make expression non greedy but its not returning anything.

Code
pattern = re.compile(r'\b[A-Za-z0-9._%.?-] @[A-Za-z0-9.-] \.[A-Z|a-z]{2,}\b')

matches = pattern.finditer(Clean)

for match in matches:
   print(match.group(0))
Output
[email protected]
[email protected]
Expected Output
[email protected]

CodePudding user response:

I guess what you are looking for is:

pattern = re.compile(r"[a-zA-Z] [\w.-]*@[a-z] [.][a-z] ")

[a-zA-Z] -> the email starts with a letter

\w -> any letter, digit or an underscores

[\w.-]* -> 0 or more letters, digits, underscores, ., or -

[a-z] -> a lower case domain 1 or more characters long

[.] -> put the . in square brackets, cuz . has a special meaning in regex

pattern = re.compile(r"[a-zA-Z] [\w.-]*@[a-z] [.][a-z] ")
matches = pattern.findall("[email protected] ooo11..com [email protected] [email protected]")
print(matches)

Result:

['[email protected]', '[email protected]', '[email protected]']

  • Related