Python全系列 教程
3567个小节阅读:5930.9k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
大多数字、字母和符号都会简单地匹配自身。例如,正则表达式 test
将会精确地匹配到 test
提示
也可以不区分大小写,后面介绍
但该规则有例外,有些字符是特殊的,并不匹配自身。因为这些字符,有表达式中,有特殊含义!
xxxxxxxxxx
. ^ $ * + ? { } [ ] \ | ( )
如果想匹配这些内容,只需要在符号前上1个\
就可以了,例如:匹配$
, 就可以写成\$
代码 | 功能 |
---|---|
. | 匹配任意1个字符(除了\n) |
[ ] | 匹配[ ]中列举的字符 |
\d | 匹配数字,即0-9 |
\D | 匹配非数字,即不是数字 |
\s | 匹配空白,即 空格,tab键 |
\S | 匹配非空白 |
\w | 匹配非特殊字符,即a-z、A-Z、0-9、_、汉字 |
\W | 匹配特殊字符,即非字母、非数字、非汉字 |
xxxxxxxxxx
import re
ret = re.match(".","M")
print(ret.group())
ret = re.match("t.o","too")
print(ret.group())
ret = re.match("t.o","two")
print(ret.group())
运行结果:
xxxxxxxxxx
M
too
two
xxxxxxxxxx
import re
# 如果hello的首字符小写,那么正则表达式需要小写的h
ret = re.match("h","hello Python")
print(ret.group())
# 如果hello的首字符大写,那么正则表达式需要大写的H
ret = re.match("H","Hello Python")
print(ret.group())
# 大小写h都可以的情况
ret = re.match("[hH]","hello Python")
print(ret.group())
ret = re.match("[hH]","Hello Python")
print(ret.group())
ret = re.match("[hH]ello Python","Hello Python")
print(ret.group())
# 匹配0到9第一种写法
ret = re.match("[0123456789]Hello Python","6Hello Python")
print(ret.group())
# 匹配0到9第二种写法
ret = re.match("[0-9]Hello Python","6Hello Python")
print(ret.group())
ret = re.match("[0-35-9]Hello Python","6Hello Python")
print(ret.group())
# 下面这个正则不能够匹配到数字4,因此ret为None
ret = re.match("[0-35-9]Hello Python","4Hello Python")
# print(ret.group())
运行结果:
xxxxxxxxxx
h
H
h
H
Hello Python
6Hello Python
6Hello Python
6Hello Python
xxxxxxxxxx
import re
# 普通的匹配方式
ret = re.match("python2","python2停止维护了")
print(ret.group())
ret = re.match("python3","python3发布了")
print(ret.group())
# 使用\d进行匹配
ret = re.match("python\d","python2停止维护了")
print(ret.group())
ret = re.match("python\d","python3发布了")
print(ret.group())
运行结果:
xxxxxxxxxx
python2
python3
python2
python3
xxxxxxxxxx
import re
match_obj = re.match("\D", "f")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
运行结果:
xxxxxxxxxx
f
xxxxxxxxxx
import re
# 空格属于空白字符
match_obj = re.match("hello\sworld", "hello world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
# \t 属于空白字符
match_obj = re.match("hello\sworld", "hello\tworld")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
运行结果:
xxxxxxxxxx
hello world
hello world
xxxxxxxxxx
import re
match_obj = re.match("hello\Sworld", "hello&world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
match_obj = re.match("hello\Sworld", "hello$world")
if match_obj:
result = match_obj.group()
print(result)
else:
print("匹配失败")
运行结果:
xxxxxxxxxx
hello&world
hello$world
xxxxxxxxxx
import re
# 匹配非特殊字符中的一位
match_obj = re.match("\w", "A")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
执行结果:
xxxxxxxxxx
A
xxxxxxxxxx
# 匹配特殊字符中的一位
match_obj = re.match("\W", "&")
if match_obj:
# 获取匹配结果
print(match_obj.group())
else:
print("匹配失败")
执行结果:
xxxxxxxxxx
&
实时效果反馈
1. 匹配字符中.
的作用是:
A 表示匹配任意1个字符(除了\n)
B 表示匹配一个数字
C 表示匹配一个空白字符
D 匹配一个非特殊字符
2. 匹配字符中\s
的作用是:
A 表示匹配任意1个字符(除了\n)
B 表示匹配一个数字
C 表示匹配一个空白字符
D 匹配一个非特殊字符
答案
1=>A 2=>C