Python编程:re中findall()用法

        在re中,(re.findall(pattern, string, flags=0)):返回string中所有与pattern相匹配的全部字符串,得到数组。其用法大致分为四类,如下:

(1)r:查找string中出现r标识的字串

import re
text = "https://mp.csdn.net/postedit/82865219"
a = re.findall(r"pos", text)
#输出 
a = ['pos']

(2)^:匹配以^标识开头的字符串;$:匹配以$标识结束的字符串

import re
text1 = "https://mp.csdn.net  wwww "
text2 = "blog.csdn.net"
a1 = re.findall(r"^https", text1)
a2 = re.findall(r"^https", text2)
a3 = re.findall(r"$net", text1)
a4 = re.findall(r"$net", text2)
#输出 
a1 = ['https']
a2 = []
a3 = []
a4 = ['net']

(3)匹配括号中的其中一个字符

import re
text = "I am so happy! "
a1 = re.findall("[a-zA-Z]", text)
a2 = re.findall("[a-zA-Z]+", text)
 
#输出
a1 = ['I', 'a', 'm', 's', 'o', 'h', 'a', 'p', 'p', 'y']
a2 = ['I', 'am', 'so', 'happy']

(4)\d:匹配0到9之间的数字;\D:匹配除0到9之外的字符

import re
text = "https://mp.csdn.net/postedit/82865219"
a1 = re.findall("\d", text)
a2 = re.findall("\d\d", text)
a3 = re.findall("\D", text)
a4 = re.findall("\D+", text)
#输出 
a1 = ['8', '2', '8', '6', '5', '2', '1', '9']
a2 = ['82', '86', '52', '19']
a3 = ['h', 't', 't', 'p', 's', ':', '/', '/', 'm', 'p', '.', 'c', 's', 'd', 'n', '.', 'n', 'e', 't', '/', 'p', 'o', 's', 't', 'e', 'd', 'i', 't', '/']
a4 = <class 'list'>: ['https://mp.csdn.net/postedit/']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值