如何编写正则表达式匹配电话、手机、邮箱和网址?

2026-03-30 08:540阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计531个文字,预计阅读时间需要3分钟。

如何编写正则表达式匹配电话、手机、邮箱和网址?

找到一款好用的正则表达式匹配电话和手机号码的方法,感谢,以下分享一些,希望能帮助更多人:

1. 匹配电话号码:pythonimport re

def match_phone_number(text): pattern=r'\b\d{3}-\d{3}-\d{4}\b' return re.findall(pattern, text)

text=我的电话号码是123-456-7890,手机号码是138-8888-8888。print(match_phone_number(text))

2. 匹配手机号码:pythonimport re

def match_mobile_number(text): pattern=r'\b1[3-9]\d{9}\b' return re.findall(pattern, text)

text=我的手机号码是138-8888-8888。print(match_mobile_number(text))

3. 匹配URL地址:pythonimport re

def match_url(text): pattern=r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' return re.findall(pattern, text)

text=请访问我的网站:http://www.example.com。

阅读全文

本文共计531个文字,预计阅读时间需要3分钟。

如何编写正则表达式匹配电话、手机、邮箱和网址?

找到一款好用的正则表达式匹配电话和手机号码的方法,感谢,以下分享一些,希望能帮助更多人:

1. 匹配电话号码:pythonimport re

def match_phone_number(text): pattern=r'\b\d{3}-\d{3}-\d{4}\b' return re.findall(pattern, text)

text=我的电话号码是123-456-7890,手机号码是138-8888-8888。print(match_phone_number(text))

2. 匹配手机号码:pythonimport re

def match_mobile_number(text): pattern=r'\b1[3-9]\d{9}\b' return re.findall(pattern, text)

text=我的手机号码是138-8888-8888。print(match_mobile_number(text))

3. 匹配URL地址:pythonimport re

def match_url(text): pattern=r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' return re.findall(pattern, text)

text=请访问我的网站:http://www.example.com。

阅读全文