0

so i want to detect email with some specific conditions

  1. start with letter or number [0-9a-zA-Z]
  2. before the @ it have to be also a letter or a number [0-9a-zA-Z]
  3. the email can contain a point in the middle of it but can't contain more than 1 point for example a1bc.2def@gmail.com is correct but a1bc..2def@gmail.com is not correct
  4. condition the email have to contain 6 minimum until 30 characters maximum
emailText='allo0..oui@gmail.com allo.non@gmail.com'

emailRegex=re.compile(r'[^.][0-9a-z]+[\.]?[0-9a-z]+@gmail.com',re.I)
matchEmail=emailRegex.findall(emailText)
print(matchEmail)

I expected that it will detect only the 2nd email allo.non@gmail.com but it detected also a part of the 1st email oui@gmail.com

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • You are aware that [dots don't matter in a gmail addresses](https://support.google.com/mail/answer/7436150?hl=en)? So you could just remove all dots before the @. – Friedrich Mar 08 '23 at 14:14
  • 1
    Does this answer your question? [Regular Expression - Validate Gmail addresses](https://stackoverflow.com/questions/16200965/regular-expression-validate-gmail-addresses) – 0stone0 Mar 08 '23 at 14:21
  • More fun facts about gmail, googlemail.com is also a valid domain for gmail. – kutschkem Mar 08 '23 at 14:44

3 Answers3

0

A basic email regex is built into HTML5, and uses the following expression:

<input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" />

This regex will match most common email formats, but it has some limitations. For example, it does not allow uppercase letters or special characters like “+” or “=” in the local part of the email (before the “@”). It also does not check for valid top-level domains (TLDs) like “.com” or “.org”.

If you want a more complex and accurate regex for email validation, you can use this one2:

<input type="email" name="email" pattern="(?=.{6,30}$)(?=[a-zA-Z0-9][\w.-]*@)[\w.-]+(?<=@)[a-zA-Z0-9][\w.-]*\.[a-zA-Z]{2,}" />

This regex will match emails that:

  • have 6 to 30 characters in total
  • start with a letter or a digit
  • have only letters, digits, dots, hyphens or underscores in the local part
  • have only one dot in the local part
  • have at least one dot after the “@”
  • end with a TLD of at least two letters

Using this regex on your emailText variable will only match “allo.non@gmail.com”, as expected.

Also

import re

emailText = 'allo0..oui@gmail.com allo.non@gmail.com'
emailRegex = re.compile(r'^[0-9a-zA-Z][0-9a-zA-Z\.]{4,28}[0-9a-zA-Z]@[0-9a-zA-Z][0-9a-zA-Z\.]*[0-9a-zA-Z]$')
matchEmail = emailRegex.findall(emailText)
print(matchEmail)

I hope this helps you with your task. Good luck!

mikeforonda
  • 114
  • 5
0
import re

r = re.compile(r"([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])")

def isValid(email):
    if re.fullmatch(r, email):
        print("Valid")
    else:
        print("Invalid")

isValid('foo+bar@gmail.com')
#Valid

isValid('a1bc..2def@gmail.com')
#Invalid

Please let me know if this is working fine.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

so using some part of the previous answer i finally get the results that i wanted thanks to everyone

import re
    
    emailText = 'OKelallo.non@gmail.com. adel allo.ff@gmail.OK  allo...@gmail.com OKloauu@gmail.com;adeee :amaterasu@gmail.com a1111douloauu@gmail.cOK'
    emailRegex = re.compile(r'[\^|\^\w][0-9a-zA-Z][0-9a-zA-Z\.]{4,28}[0-9a-zA-Z]@[0-9a-zA-Z][0-9a-zA-Z]*\.[0-9a-zA-Z]{1,4}[\$|\^\w]')
    matchEmail = emailRegex.findall(emailText)
    print(matchEmail)