python正则

Python匹配在我们使用的时候有很多的注意事项。我们在不断的学习中会遇到不少的问题。下面我们就详细的看看如何才能更好的掌握相关的Python匹配技术问题。用法2的正则表达式对象版本

  1. rereobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束  
  2. if reobj.match(subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

创建一个正则表达式对象,然后通过该对象获得Python匹配细节

 

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. # match start: match.start()  
  5. # match end (exclusive): match.end()  
  6. # matched text: match.group()  
  7. do_something()  
  8. else:  
  9. do_anotherthing() 

用正则表达式对象获取Python匹配子串

 

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group()  
  5. else:  
  6. result = "" 

用正则表达式对象获取 捕获组所Python匹配的子串

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group(1)  
  5. else:  
  6. result = "" 

用正则表达式对象获取 有名组所Python匹配的子串

  1. rereobj = re.compile(regex)  
  2. match = reobj.search(subject)  
  3. if match:  
  4. result = match.group("groupname")  
  5. else:  
  6. result = "" 

用正则表达式 对象获取所有Python匹配子串并放入数组

 

  1. rereobj = re.compile(regex)  
  2. result = reobj.findall(subject) 

通过正则表达式对象遍历所Python有匹配子串

 

  1. rereobj = re.compile(regex)  
  2. for match in reobj.finditer(subject):  
  3. # match start: match.start()  
  4. # match end (exclusive): match.end()  
  5. # matched text: match.group 

以上就是对Python匹配的相关细节介绍。

转载于:https://www.cnblogs.com/vampirejt/p/python.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值