一、首先考虑什么场景下会使用到正则表达式
场景一:比如有个需求,判断用户输入的密码是否是6-15位的?
场景二:需要将new_str=‘Today126782is83272Monday’,转化成Today is Monday,如何快速实现?
场景三:自动化测试中需要将用例中特定的内容进行参数化,如何实现?
场景四:web自动化要匹配前端代码中是否有某个内容,如何实现?
…
二、正则表达式是什么
正则表达式是:
- 一串描述文本规则的代码
- 可进行复杂的字符串搜索和替换
- 任何编程语言都支持
- 绝大多数文本编辑器都支持
三、正则表达式如何使用
匹配单个字符
常用匹配单个字符表达式:
字符 | 功能 |
---|---|
. | 匹配任意1个字符(除了\n) |
[ ] | 匹配[ ]中列举的字符 |
\d | 匹配数字,即0-9 |
\D | 匹配非数字,即不是数字 |
\s | 匹配空白,即空格,tab键 |
\S | 匹配非空白 |
\w | 匹配单词字符,即a-z、A-Z、0-9、_ |
\W | 匹配非单词字符 |
操作演练:
1a. 匹配任意字符:.
one_str = "Today is Monday!"
result = re.match("T.day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Today
1b. 匹配任意字符时\n不能匹配到
one_str = "T\noday is Monday!"
result = re.match("T.day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:未匹配上!
2. 匹配[]中列举的字符
one_str = "Today is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match("T[abo]day", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Today
3. 匹配数字,即0-9
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"Today\d", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Today2
4. 匹配单词字符
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"T\wday\d", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Today2
匹配多个字符
常用匹配单个字符表达式:
字符 | 功能 |
---|---|
* | 匹配前一个字符出现0次或者无限次,即可有可无 |
+ | 匹配前一个字符出现1次或者无限次,即至少有1次 |
? | 匹配前一个字符出现1次或者0次,即要么有1次要么没有 |
{m} | 匹配前一个字符出现m次 |
{m, n} | 匹配前一个字符出现从m到n次 |
操作演练
1. 至少匹配一次:+
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"Today2\w+", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:未匹配上!
2. 要么匹配一次要么匹配0次:?
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"Today2\w?", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Today2
3. 匹配{m}次
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"T\w{5}", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
匹配5次后,运行结果:Today2
4. 匹配{m,n}次
one_str = "Today2 is Monday!"
# 使用match方法来匹配
# 如果能正常匹配,则返回match,并且group()
# 如果不能匹配,则返回的None
result = re.match(r"T\w{1,3}", one_str)
if result:
print(result.group())
else:
print("未匹配上!")
如上代码,匹配{1,3},运行结果:Toda
四、其他常见方法
1. search方法
查找特定字符串是否存在(只要找到字符就返回)
one_str = "Today2 is Monday!"
result = re.search(r'Mon\w+', one_str)
if result:
print(result.group())
else:
print("未匹配上!")
运行结果:Monday
2. sub方法
将匹配到的数据进行替换
one_str = "Today is Monday!"
result = re.sub(r'Mon\w+', 'Tuesday', one_str)
if result:
print(result)
else:
print("未匹配上!")
运行结果:Today is Tuesday!
3. split方法
根据匹配到的特定分隔符进行切割字符串,并返回一个列表
例如:将下列字符串分割后显示:Today is Monday
target_str = 'Today126782is83272Monday'
new_list = re.split(r'\d+', target_str) # 使用正则匹配后进行分割
new_str = (' '.join(new_list))
print(new_str)
运行结果:Today is Monday