“”"
一、正则表达式:
匹配指定规则的字符串:
二:使用:
1,单字符匹配
2,多字符匹配
3,逻辑运算|
4,边界值
5,分组匹配
“”"
import re
#分组匹配
#接口响应结果
response = {“key1”:“value”,“key2”:“value2”}
#要去接口响应结果里面提取的参数的key
test_demo = ‘{“#key1#:value,#key2#:value2”}’
res=re.findall(“#(\w.+?)#”,test_demo)
print(res)
for i in res:
print(response[i])
边界值
#: $ :匹配结尾处
test_str = “hello python”
res = re.findall(“on$”,test_str)
print(res)
#: ^ :匹配以he开头的
test_str = "hello python "
res = re.findall(“^he”,test_str)
print(res)
#################################################33
| 或运算
test_str = "hello python "
res = re.findall(“he|py|th”,test_str)
print(res)
############################################################
{n,m} 匹配前面一个字符出现n次到m次
test_str = “good go doo gooo”
res = re.findall(“go{1,3}”,test_str)
print(res)
{n} 匹配前面一个字符出现n次
test_str = “good go doo gooo”
res = re.findall(“go{3}”,test_str)
print(res)
?:匹配前一个字符出现1次或者无限次
test_str = “good go doo gooo”
res = re.findall(“go?”,test_str)
print(res)
+ :匹配前一个字符出现1次或者无限次
test_str = “good go doo gooo”
res = re.findall(“go+”,test_str)
print(res)
#多字符匹配。未匹配到返回空,最后一位会多匹配一次
test_str = “pythonpppppppppl8976onp”
res = re.findall(“p*”,test_str)
print(res)
################################################################
#单字符匹配
\W (大写)匹配特殊字符
test_str = “#$_*@!()[]1234中文”
res = re.findall(“\W”,test_str)
print(res)
\w (小写)匹配非特殊字符
test_str = “#$_*@!()[]1234中文”
res = re.findall(“\w”,test_str)
print(res)
\S 匹配非空白
#用例去字符串中间的空格