正则表达式

在这里插入图片描述
“”"
一、正则表达式:
匹配指定规则的字符串:

二:使用:
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 匹配非空白

#用例去字符串中间的空格

test_str = “py thon pl”

res = re.findall(“\S”,test_str)

print(res)

print(‘’.join(res))

\s 匹配空白,空格,tab键

test_str = “p yth onpl 3456 7on”

res = re.findall(“\s”,test_str)

print(res)

\D 匹配非数字

test_str = “pythonpl8976on”

res = re.findall(“\D”,test_str)

print(res)

\d 匹配数字,返回list

test_str = “pythonpl786”

res = re.findall(“\d”,test_str)

print(res)

[] 匹配任意一个字符,并不是一个整体,返回list

test_str = “pythonpl”

res = re.findall(“[phe]”,test_str)

print(res)

. 匹配p后面任意一个字符,返回list

test_str = “pythonpl”

res = re.findall(“p.”,test_str)

print(res)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值