In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要为大家分享python中正则表达式分组和字符串匹配的使用方法。文中还介绍了使用findall和finditer函数查找所有匹配正则表达式的字符串的方法,希望大家通过这篇文章能有所收获。
# 分组# 正则表达式中用一对圆括号括起来的部分被称为一个分组# '(\d\d\d)-(\d\d)'import re'''1. 只有圆括号括起来的部分才算一组,如果正则表达式中既有被圆括号括起来的部分,也有未被圆括号括起来的部分,那么只将圆括号括起来的部分算一组2. group方法,如果不指定参数,会返回匹配的整个字符串,如果加参数,会返回指定分组的字符串,组索引从1开始3. groups方法,以元组形式返回匹配的所有分组4. 分组的索引是从1开始的 '''# 分3组 3个数字-4个数字-2个小写字母m = re.match('(\d{3})-\d{4}-[a-z]{2}','123-4567-xy')print(m) # print(m.groups()) # ('123',)print(m.group()) # 123-4567-xyprint(m.group(1)) # 123print('-------------')m = re.match('(\d{3})-(\d{4})-[a-z]{2}','123-4567-xy')print(m) # print(m.groups()) # ('123', '4567') 这个是返回一个元组 只要是分组里面的就放在元组里print(m.group()) # 123-4567-xyprint(m.group(1)) # 123print(m.group(2)) # 456print('----------')m = re.match('(\d{3})-(\d{4})-([a-z]{2})','123-4567-xy')print(m) # print(m.groups()) # ('123', '4567', 'xy')print(m.group()) # 123-4567-xyprint(m.group(1)) # 123print(m.group(2)) # 456print(m.group(3)) # xyprint('-----------')m = re.match('(\d{3}-\d{4})-([a-z]{2})','123-4567-xy')print(m) # print(m.groups()) # ('123-4567', 'xy')print(m.group()) # 123-4567-xyprint(m.group(1)) # 123-4567print(m.group(2)) # xy# 匹配字符串的起始和结尾以及单词边界'''"^":匹配字符串的开始"$":匹配字符串的结束"\b":匹配单词边界单词边界:是指空格或标点符号" hello?" 这个左右2侧都有边界" world0" 这个表示左侧有边界,右侧没有边界'''import re# "The"必须在字符串的开始才会搜索到m = re.search('The', 'abc The.')print(m) # m = re.search('^The', 'The bus.')print(m) # # The必须在字符串的结尾才会搜索到m = re.search('The$','The end.')print(m) # Nonem = re.search('The$','end.The')print(m) # print("---------------")# 要求'this'左侧必须有边界才能搜索到m = re.search(r'\bthis',"What's this?") # 不使用r里面的\b就会给你转义了print(m) # m = re.search(r'\bthis',"this is a bus.")print(m) # m = re.search(r'\bthis'," is a bus.")print(m) # m = re.search(r'\bthis',"1234this is a bus.")print(m) # Nonem = re.search(r'\bthis\b',"What's this123")print(m) # None# 使用findall和finditer函数查找所有匹配正则表达式的字符串'''findall:通过列表返回所有满足条件的字符串,DOM 这是所有结果一起返回finditer:将搜索结果通过一个迭代器返回,SAX 这个不是所有的返回'''import res = '12-a-abc54-a-xyz---78-A-ytr'result = re.findall(r'\d\d-[a]-[a-z]{3}',s)print(result) # ['12-a-abc', '54-a-xyz']result = re.findall(r'\d\d-[aA]-[a-z]{3}',s) # 这一步里面的 [aA] 也是忽略大小写print(result) # ['12-a-abc', '54-a-xyz', '78-A-ytr'] result = re.findall(r'(\d\d-[aA])-([a-z]{3})',s) # 分组返回 拆成一个组print(result) # [('12-a', 'abc'), ('54-a', 'xyz'), ('78-A', 'ytr')]s1 = '12-a-abc54-a-xYz---78-A-ytr'result = re.findall(r'\d\d-a-[a-z]{3}',s1,re.I) # 可以加第三个参数 第三个参数的位置 可以忽略大小写 re.I 这个就是忽略大小写print(result) # ['12-a-abc', '54-a-xYz', '78-A-ytr']it = re.finditer(r'(\d\d)-a-([a-z]{3})',s,re.I)for result in it: print(result.group(),end=' ')'''12-a-abc 54-a-xyz 78-A-ytr '''
以上就是python中正则表达式分组和字符串匹配的使用方法,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注行业资讯!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.