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

2026-03-30 08:541阅读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。print(match_url(text))

4. 复制代码:pythondef copy_code(code): import pyperclip pyperclip.copy(code)

code=print('Hello, world!')copy_code(code)

5. 代码示例:pythondef print_code(code): print(code)

code=import 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))print_code(code)

找到一个好用的正则匹配电话和手机号的方法,感谢,在这里分享一下,希望可以帮助更多人

匹配全部url地址和链接内容
复制代码 代码如下:
$str = "<a href='www.baidu.com' target='_blank'>百度</a>www<a href='www.sina.com' target='_blank'>新浪</a>kod";

preg_match_all("/<a href=([\"\'])(http:\/\/([\w\d\.])+)[^>]*>(.*?)<\/a>/i", $str, $matches);

echo "<pre>";
var_dump($matches[2]);
echo "<br />";
var_dump($matches[4]);

匹配邮箱
复制代码 代码如下:
$emails = "wangzhanu@126.comha12wangzhanu@163.com";

preg_match_all("/([a-z0-9_\-\.]+)@(([a-z0-9]+[_\-]?)\.)+[a-z]{2,3}/i", $emails, $matches);

var_dump($matches[0]);

匹配手机以及电话号码
复制代码 代码如下:
$tel = "13911112222sf010-44444442dfg18811112222";

preg_match_all("/1[3,5,8]{1}[0-9]{1}[0-9]{8}|0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{1,4})?/", $tel, $matches);

var_dump($matches);

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

本文共计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。print(match_url(text))

4. 复制代码:pythondef copy_code(code): import pyperclip pyperclip.copy(code)

code=print('Hello, world!')copy_code(code)

5. 代码示例:pythondef print_code(code): print(code)

code=import 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))print_code(code)

找到一个好用的正则匹配电话和手机号的方法,感谢,在这里分享一下,希望可以帮助更多人

匹配全部url地址和链接内容
复制代码 代码如下:
$str = "<a href='www.baidu.com' target='_blank'>百度</a>www<a href='www.sina.com' target='_blank'>新浪</a>kod";

preg_match_all("/<a href=([\"\'])(http:\/\/([\w\d\.])+)[^>]*>(.*?)<\/a>/i", $str, $matches);

echo "<pre>";
var_dump($matches[2]);
echo "<br />";
var_dump($matches[4]);

匹配邮箱
复制代码 代码如下:
$emails = "wangzhanu@126.comha12wangzhanu@163.com";

preg_match_all("/([a-z0-9_\-\.]+)@(([a-z0-9]+[_\-]?)\.)+[a-z]{2,3}/i", $emails, $matches);

var_dump($matches[0]);

匹配手机以及电话号码
复制代码 代码如下:
$tel = "13911112222sf010-44444442dfg18811112222";

preg_match_all("/1[3,5,8]{1}[0-9]{1}[0-9]{8}|0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{1,4})?/", $tel, $matches);

var_dump($matches);

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