@全体成员 今日的正则10题:可以使用正则和非正则,两种方法实现
给定字符串s=“i Am a gOod boy baby!!”
1、判断一个字符串中字母是否全部为小写
s=“i Am a gOod boy baby!!”
import string
flag= True
for i in s:
if i not in string.ascii_lowercase:
flag=False
print(“字符串s中都是小写:”,flag)
执行结果:
字符串s中都是小写: False
import re
s=“i Am a gOod boy baby!!”
if re.search(r"[A-Z]",s):
print(“False”)
else:
print(“True”)
执行结果:
False
2、判断有多少个空格
s=“i Am a gOod boy baby!!”
count=0
for i in s:
if i ==" ":
count+=1
print(“字符串s中有%d个空格:”,count)
执行结果:
字符串s中有%d个空格: 6
import re
s=“i Am a gOod boy baby!!”
count1 = re.findall(r"\W",s)
count2 = re.findall(r"!",s)
count = len(count1)-len(count2)
print(“字符串s中有%d个空格:”,count)
执行结果:
字符串s中有%d个空格: 6
3、求大写字母的个数
s=“i Am a gOod boy baby!!”
import string
count=0
for i in s:
if i in string.ascii_uppercase:
count+=1
print(“字符串s中大写个数:”,count)
执行结果:
字符串s中大写个数: 2
import re
s=“i Am a gOod boy baby!!”
count = re.findall(r"[A-Z]",s)
print(“字符串s中大写个数:”,len(count))
执行结果:
字符串s中大写个数: 2
4、匹配小写字母开头的单词,计算数量
s=“i Am a gOod boy baby!!”
import string
result=[]
for i in s:
if i not in string.ascii_letters:
s1=s.replace(i," ")
s=s1
for j in s.split():
if j[0] in string.ascii_lowercase:
result.append(j)
print(“小写字母开头的单词为%s,个数为%d”%(result,len(result)))
执行结果:
小写字母开头的单词为[‘i’, ‘a’, ‘gOod’, ‘boy’, ‘baby’],个数为5
import re
s=“i Am a gOod boy baby!!”
result =[]
result1 = re.findall(r"\w+",s)
print(result1)
result2 = re.findall(r"[A-Z][A-Za-z]+",s)
print(result2)
for i in result1:
if i not in result2:
result.append(i)
print(“小写字母开头的单词为%s,个数为%d”%(result,len(result)))
执行结果:
[‘i’, ‘Am’, ‘a’, ‘gOod’, ‘boy’, ‘baby’]
[‘Am’, ‘Ood’]
小写字母开头的单词为[‘i’, ‘a’, ‘gOod’, ‘boy’, ‘baby’],个数为5
import re
s=“i Am a gOod boy baby!!”
result = re.findall(r"\b[a-z]\w*",s)
print(result)
执行结果:
[‘i’, ‘a’, ‘gOod’, ‘boy’, ‘baby’]
5、查找所有单词
s=“i Am a gOod boy baby!!”
import string
for i in s:
if i not in string.ascii_letters:
s1=s.replace(i," ")
s=s1
print(“单词为%s,个数为%d”%(s.split(),len(s.split())))
执行结果:
单词为[‘i’, ‘Am’, ‘a’, ‘gOod’, ‘boy’, ‘baby’],个数为6
import re
s=“i Am a gOod boy baby!!”
result = re.findall(r"\w+",s)
print(“单词为%s,个数为%d”%(result,len(result)))
执行结果:
单词为[‘i’, ‘Am’, ‘a’, ‘gOod’, ‘boy’, ‘baby’],个数为6
6、匹配以b开头的单词
s=“i Am a gOod boy baby!!”
import string
result=[]
for i in s:
if i not in string.ascii_letters:
s1=s.replace(i," ")
s=s1
for j in s.split():
if j[0]==“b”:
result.append(j)
print(“b字母开头的单词为%s,个数为%d”%(result,len(result)))
执行结果:
b字母开头的单词为[‘boy’, ‘baby’],个数为2
import re
s=“i Am a gOod boy baby!!”
result = re.findall(r"b[A-Za-z]+",s)
print(“b字母开头的单词为%s,个数为%d”%(result,len(result)))
执行结果:
b字母开头的单词为[‘boy’, ‘baby’],个数为2
7、匹配s='123@qq.comaaa@163.combbb@126.comasdfasfs33333@adfcom’中的邮箱
s=‘123@qq.comaaa@163.combbb@126.comasdfasfs33333@adf.com’
import re
print(re.findall(r"\w+@\w+.com",s))
执行结果:
[‘123@qq.com’, ‘aaa@163.com’, ‘bbb@126.com’, ‘asdfasfs33333@adf.com’]
8、匹配浮点数,如“8.256”
s=“8.256,9.2345,10.390”
import re
print(re.findall(r"[0-9].[0-9]{3}",s))
执行结果:
[‘8.256’, ‘9.234’, ‘0.390’]
s=“8.256,9.2345,10.390,18.82567”
import re
print(re.findall(r"8.256",s))
执行结果:
[‘8.256’]
9、匹配时间格式“xx年-xx月-xx日”
s=“2018年-09月-06日我们2018年-09月-01日拍的品牌2018年-09月-02日品牌店铺2018年-09月-04日……&992018年-09月-09日”
import re
print(re.findall(r"\d{4}年-\d{2}月-\d{2}日",s))
执行结果:
[‘2018年-09月-06日’, ‘2018年-09月-01日’, ‘2018年-09月-02日’, ‘2018年-09月-04日’, ‘2018年-09月-09日’]
10、匹配s=“f2sf f3sf r4re pp34f p2op” 匹配出单词,并且匹配出第二位为2的单词
s=“f2sf f3sf r4re pp34f p2op”
import re
print(re.findall(r"\w2\w+",s))
print(re.findall(r"\w2\w*",s))
执行结果:
[‘f2sf’, ‘p2op’]
[‘f2sf’, ‘p2op’]
11、把s这个列表中的元素按照日期的大小做排序
s= [{‘日期’: ‘2018-09-04’, ‘测试1’: ‘50.00 %’, ‘测试2’: ‘100.00%’}, {‘日期’: ‘2018-09-05’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-06’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘100.00%’}, {‘日期’: ‘2018-08-31’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-01’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-02’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-03’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}]
s= [{‘日期’: ‘2018-09-04’, ‘测试1’: ‘50.00 %’, ‘测试2’: ‘100.00%’}, {‘日期’: ‘2018-09-05’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-06’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘100.00%’}, {‘日期’: ‘2018-08-31’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-01’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-02’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-03’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}]
def func(m):
return m[‘日期’]
s.sort(key=func)
print(s)
执行结果:
[{‘日期’: ‘2018-08-31’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-01’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-02’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-03’, ‘测试1’: ‘无执行’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-04’, ‘测试1’: ‘50.00 %’, ‘测试2’: ‘100.00%’}, {‘日期’: ‘2018-09-05’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘无执行’}, {‘日期’: ‘2018-09-06’, ‘测试1’: ‘100.00%’, ‘测试2’: ‘100.00%’}]