每日一题 【每日一题】匹配邮箱地址 -Python-20210910

Jack · 2021年09月11日 · 最后由 lework 回复于 2021年12月29日 · 29 次阅读
本帖已被设为精华帖!

使用正则匹配邮箱地址。

Jack 将本帖设为了精华贴 09月11日 18:22

参考代码:

import re
email_list= ["test01@163.com","test02@163.123", ".test03g@qq.com", "test04@gmail.com" ]
for email in email_list:
  ret = re.match("[\w]{4,20}@(.*)\.com$",email)
  if ret:
    print("%s 是符合规定的邮件地址,匹配后结果是:%s" % (email,ret.group()))
  else
    print("%s 不符合要求" % email)
package main

import (
    "fmt"
    "regexp"
)

func IsEmail(s string) bool {
    pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
    reg := regexp.MustCompile(pattern)
    return reg.MatchString(s)
}

func main() {
    /*
    匹配邮箱地址
    */

    email := []string{"test01@163.com","test02@163.123", ".test03g@qq.com", "test04@gmail.com"}

    for _, val :=  range email {
        if is := IsEmail(val); is {
            fmt.Printf("%s 是符合规定的邮件地址\n",val)
        } else {
            fmt.Printf("%s 不符合要求\n",val)
        }

    }
}
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册