赞
踩
目录
- data=['1_aa',"2_bbb","1_xxx"]
-
- prexs=["1_",'2_','3_']
-
- data2=[[] for _ in range(len(prexs))]
-
- for aaa in data:
-
- for index, prex in enumerate(prexs):
- if aaa.startswith(prex):
- data2[index].append(aaa)
-
- for data_item in data2:
- print(len(data_item))
- import re
- data = ['1_aa', "2_bbb", "1_xxx"]
-
- prexs = '1_|2_|3_'
-
- for aaa in data:
- result=re.match(prexs, aaa)
- if (result):
- print(result.span())
- import re
- data = ['1_aa', "2_bbb", "3_xxx","aayyaa"]
-
- prexs = '1_1|2_|3_|yy'
-
- for aaa in data:
- result=re.search(prexs, aaa)
- if (result):
- print(result.span())
- import re
-
- txt = "标准型aaabbbcccdddCIR"
- result = re.search('大功率.*CIR|标准型.*CIR|小型化.*CIR',txt)
-
- print(result is None)
- print(result)
match()函数只检测RE是不是在string的开始位置匹配,
search()会扫描整个string查找匹配
match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none
举例说明:
import re
print(re.match('super', 'superstition').span())
原文链接:https://blog.csdn.net/weixin_38819889/article/details/93846579
- import re
- data = ['1_aa', "2_bbb", "3_xxx"]
-
- prexs = '1_1*|2_|3_'
-
- for aaa in data:
- result=re.search(prexs, aaa)
- if (result):
- print(result.span())
结果:
(0, 2)
(0, 2)
(0, 2)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。