请给我30个python语言下的正则表达式的代码实例
时间: 2024-02-09 07:07:27 浏览: 77
学会Python正则表达式,就看这20个例子(脚本之家修正版)
好的,以下是30个Python语言下的正则表达式的代码实例:
1. 匹配手机号码
```python
import re
phone_number = "13812345678"
pattern = r"^1[3456789]\d{9}$"
result = re.match(pattern, phone_number)
if result:
print("是手机号码")
else:
print("不是手机号码")
```
2. 匹配电子邮箱地址
```python
import re
email = "example@mail.com"
pattern = r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
result = re.match(pattern, email)
if result:
print("是电子邮箱地址")
else:
print("不是电子邮箱地址")
```
3. 匹配IP地址
```python
import re
ip_address = "192.168.0.1"
pattern = r"^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$"
result = re.match(pattern, ip_address)
if result:
print("是IP地址")
else:
print("不是IP地址")
```
4. 匹配日期格式
```python
import re
date = "2022-01-01"
pattern = r"^\d{4}-\d{2}-\d{2}$"
result = re.match(pattern, date)
if result:
print("是日期格式")
else:
print("不是日期格式")
```
5. 匹配数字
```python
import re
num = "123456"
pattern = r"^\d+$"
result = re.match(pattern, num)
if result:
print("是数字")
else:
print("不是数字")
```
6. 匹配字母
```python
import re
letter = "abcde"
pattern = r"^[a-zA-Z]+$"
result = re.match(pattern, letter)
if result:
print("是字母")
else:
print("不是字母")
```
7. 匹配字母和数字
```python
import re
string = "abc123"
pattern = r"^[a-zA-Z0-9]+$"
result = re.match(pattern, string)
if result:
print("是字母和数字")
else:
print("不是字母和数字")
```
8. 匹配中文
```python
import re
chinese = "中文"
pattern = r"^[\u4e00-\u9fa5]+$"
result = re.match(pattern, chinese)
if result:
print("是中文")
else:
print("不是中文")
```
9. 匹配用户名
```python
import re
username = "user_123"
pattern = r"^[a-zA-Z][a-zA-Z0-9_]{5,17}$"
result = re.match(pattern, username)
if result:
print("是用户名")
else:
print("不是用户名")
```
10. 匹配密码
```python
import re
password = "123456"
pattern = r"^[a-zA-Z]\w{5,17}$"
result = re.match(pattern, password)
if result:
print("是密码")
else:
print("不是密码")
```
11. 匹配URL
```python
import re
url = "https://www.example.com"
pattern = r"^https?://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"
result = re.match(pattern, url)
if result:
print("是URL")
else:
print("不是URL")
```
12. 匹配HTML标签
```python
import re
html = "<div class='example'>Hello, World!</div>"
pattern = r"<[^>]+>"
result = re.findall(pattern, html)
print(result)
```
13. 匹配XML标签
```python
import re
xml = "<example>Hello, World!</example>"
pattern = r"<([a-zA-Z]+)[^>]*>(.*?)</\1>"
result = re.findall(pattern, xml)
print(result)
```
14. 匹配电话号码
```python
import re
phone_number = "010-12345678"
pattern = r"^0\d{2,3}-\d{7,8}$"
result = re.match(pattern, phone_number)
if result:
print("是电话号码")
else:
print("不是电话号码")
```
15. 匹配邮政编码
```python
import re
zipcode = "100000"
pattern = r"^[1-9]\d{5}$"
result = re.match(pattern, zipcode)
if result:
print("是邮政编码")
else:
print("不是邮政编码")
```
16. 匹配身份证号码
```python
import re
id_number = "110101199001010101"
pattern = r"^\d{6}(19|20)\d{2}(0\d|1[0-2])([0-2]\d|3[0-1])\d{3}[0-9Xx]$"
result = re.match(pattern, id_number)
if result:
print("是身份证号码")
else:
print("不是身份证号码")
```
17. 匹配用户名和密码
```python
import re
username = "user_123"
password = "password123"
pattern = r"^[a-zA-Z][a-zA-Z0-9_]{5,17}$|^[a-zA-Z]\w{5,17}$"
result1 = re.match(pattern, username)
result2 = re.match(pattern, password)
if result1 and result2:
print("是用户名和密码")
else:
print("不是用户名和密码")
```
18. 匹配QQ号码
```python
import re
qq_number = "123456789"
pattern = r"^[1-9]\d{4,10}$"
result = re.match(pattern, qq_number)
if result:
print("是QQ号码")
else:
print("不是QQ号码")
```
19. 匹配微信号
```python
import re
wechat_id = "abc123"
pattern = r"^[a-zA-Z][-_a-zA-Z0-9]{5,19}$"
result = re.match(pattern, wechat_id)
if result:
print("是微信号")
else:
print("不是微信号")
```
20. 匹配车牌号
```python
import re
license_plate = "京A12345"
pattern = r"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}\w{5}$"
result = re.match(pattern, license_plate)
if result:
print("是车牌号")
else:
print("不是车牌号")
```
21. 匹配微博账号
```python
import re
weibo_id = "@example"
pattern = r"^@[\w\u4e00-\u9fa5]+"
result = re.match(pattern, weibo_id)
if result:
print("是微博账号")
else:
print("不是微博账号")
```
22. 匹配身高
```python
import re
height = "180cm"
pattern = r"^\d{2,3}cm$"
result = re.match(pattern, height)
if result:
print("是身高")
else:
print("不是身高")
```
23. 匹配体重
```python
import re
weight = "60kg"
pattern = r"^\d{2,3}kg$"
result = re.match(pattern, weight)
if result:
print("是体重")
else:
print("不是体重")
```
24. 匹配IPV6地址
```python
import re
ipv6_address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
pattern = r"^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"
result = re.match(pattern, ipv6_address)
if result:
print("是IPV6地址")
else:
print("不是IPV6地址")
```
25. 匹配MAC地址
```python
import re
mac_address = "00:11:22:33:44:55"
pattern = r"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"
result = re.match(pattern, mac_address)
if result:
print("是MAC地址")
else:
print("不是MAC地址")
```
26. 匹配XML注释
```python
import re
xml_comment = "<!-- This is a comment -->"
pattern = r"<!--[\s\S]*?-->"
result = re.findall(pattern, xml_comment)
print(result)
```
27. 匹配HTML标签属性
```python
import re
html_tag = "<div class='example'>Hello, World!</div>"
pattern = r"([\w-]+)\s*=\s*\"([^\"]+)\""
result = re.findall(pattern, html_tag)
print(result)
```
28. 匹配RGB颜色值
```python
import re
rgb_color = "rgb(255, 255, 255)"
pattern = r"^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$"
result = re.match(pattern, rgb_color)
if result:
print("是RGB颜色值")
else:
print("不是RGB颜色值")
```
29. 匹配HSL颜色值
```python
import re
hsl_color = "hsl(0, 0%, 100%)"
pattern = r"^hsl\((\d{1,3}),\s*(\d{1,3})%,\s*(\d{1,3})%\)$"
result = re.match(pattern, hsl_color)
if result:
print("是HSL颜色值")
else:
print("不是HSL颜色值")
```
30. 匹配十六进制颜色值
```python
import re
hex_color = "#ffffff"
pattern = r"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
result = re.match(pattern, hex_color)
if result:
print("是十六进制颜色值")
else:
print("不是十六进制颜色值")
```
阅读全文