Python 正则表达式的简单使用示例
发布日期:2021-11-22 04:29:01 浏览次数:2 分类:技术文章

本文共 684 字,大约阅读时间需要 2 分钟。

1,匹配数字

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32Type "copyright", "credits" or "license()" for more information.>>> import re  #导入模块>>> a="123xxqert45xx67ghxx890">>> b=re.findall("\d+",a)   #匹配所有的数字>>> print (b)['123', '45', '67', '890']>>>

2,匹配字符串

>>> print (re.findall("\d.",a)) #匹配所有 以数字开头的 长度为2的子字符串['12', '3x', '45', '67', '89']>>> print ("---------------------------")--------------------------->>> c=re.findall("xx(.*)xx",a)  # .*    贪吃算法>>> print (c)['qert45xx67gh']>>> d=re.findall("xx(.*?)xx",a)  # .*?  非贪吃算法>>> print (d)['qert45']>>>

3,匹配字符串并替换

>>> e=re.sub("xx.","~",a)   # 匹配xx. 并替换为 ~>>> print (e)123~ert45~7gh~90>>>

转载地址:https://blog.csdn.net/zxwd2015/article/details/51262281 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:android获取今天周几的示例
下一篇:Python安装及配置环境变量的例子

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月28日 20时07分51秒