简体   繁体   English

Email 使用 re.search 进行验证 Python

[英]Email Verification using re.search Python

Need help making email verifications with the variable 'pattern' and making it so that it loops if it doesn't contain whatever is within the pattern.需要帮助使用变量“模式”进行 email 验证并使其在不包含模式中的任何内容时循环。 Required to use re.search.需要使用 re.search。 I tried a couple of things for the last hour and this is where I'm kind of lost.在过去的一个小时里,我尝试了几件事,这就是我迷路的地方。

import re
pattern = '[a-zA-Z0-9]' + '[a-zA-Z0-9]'+'@[a-zA-Z]'+'(.com/.edu/.net)'
user_input = input('Enter Your Email:')
while user_input is not pattern:
    if (re.search(pattern,user_input)):
        print(re.seach(pattern,user_input))
        print('Valid Email:'+ user_input)
    else:
        print(re.search(pattern,user_input))
        print('Invalid Email:'+ user_input)
        user_input = input('Enter Your Email:')```

The code is great, but the pattern lacks a bit of functionality.代码很棒,但模式缺少一些功能。 In fact for e-mail addresses, it misses the dash - and the underscore _ .事实上,对于电子邮件地址,它缺少破折号-和下划线_ Luckily, you can just say to match \w .幸运的是,您可以只说匹配\w It is the same as if you would have specified [a-zA-Z0-9_] .这与您指定[a-zA-Z0-9_] (it still misses the dash though, so your approach is good but too short.) Anyway, there are a few further things that an address should meet. (虽然它仍然错过了破折号,所以你的方法很好但太短了。)无论如何,地址应该满足一些进一步的要求。

  1. it must start with a alphabetic character它必须以字母字符开头
  2. While theoretically, the address could be composed of a single character at the start and only to after the @ sign, and be almost infinitely long, it is highly unlikely虽然从理论上讲,地址可以由单个字符组成,但仅在 @ 符号之后,并且几乎无限长,这是极不可能的

I suggest the pattern '[a-zA-Z]+[a-zA-Z0-9_\-]{0,42}@[a-zA-Z]{2,42}\.((com)|(edu)|.net))\b?'我建议模式'[a-zA-Z]+[a-zA-Z0-9_\-]{0,42}@[a-zA-Z]{2,42}\.((com)|(edu)|.net))\b?'

Limiting the number of characters with '{m,n}' lets you ensure that you won't have an overflow error when storing the address.使用'{m,n}'限制字符数可以确保在存储地址时不会出现溢出错误。 Well and addresses shorter than 'a@bc.st' simply don't exist as at least two characters are required.好吧,比'a@bc.st'短的地址根本不存在,因为至少需要两个字符。

Lastly, the or-operator applies only to the immediate adjoin characters, so you need to group the mail extensions: ((com)|(edu)|.net))最后,or-operator 仅适用于紧邻的字符,因此您需要对邮件扩展名进行分组: ((com)|(edu)|.net))

import re
pattern = '[a-zA-Z]+[a-zA-Z0-9_\-]{0,42}@[a-zA-Z]{2,42}\.((com)|(edu)|(net))\b?'
while True:
  user_input = input('Enter Your Email:')
  if re.match(pattern, user_input):
      print(re.search(pattern,user_input))
      print('Valid Email:'+ user_input)
      break
  else:
      print(re.match(pattern,user_input))
      print('Invalid Email:'+ user_input)

I think, it is better if you use re.match() as it matches the string right from the start.我认为,最好使用re.match()因为它从一开始就匹配字符串。 Usually one doesn't like if you end up with 1abc@def.comm to be a valid address (because re.search() would find the valid string abc@def.com . With the same argumentation, you should add a \b to the end of the pattern通常,如果您以1abc@def.comm作为有效地址结束(因为re.search()会找到有效字符串abc@def.com ),通常不会有人喜欢。使用相同的论证,您应该添加一个\b到模式的末尾

I made a slight modification to your pattern and to your code.我对您的模式和代码做了轻微修改。

import re
pattern = '[a-zA-Z0-9]+@[a-zA-Z]+(\.com|\.edu|\.net)'
while True:
  user_input = input('Enter Your Email:')
  if (re.search(pattern,user_input)):
      print(re.search(pattern,user_input))
      print('Valid Email:'+ user_input)
      break
  else:
      print(re.search(pattern,user_input))
      print('Invalid Email:'+ user_input)
     

Here's an example run:这是一个示例运行:

Enter Your Email:fred
None
Invalid Email:fred
Enter Your Email:mark@so.com
<re.Match object; span=(0, 11), match='mark@so.com'>
Valid Email:mark@so.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM