Python-正则表达式日常使用方法的记录(一)

1、匹配对象实例(1):主要匹配对应字符串中的数字。
常用方法

首先第一步:当然是导入库:
import re

'''写法1'''
phonenumber = re.compile(r"(\d\d\d)-(\d\d\d-\d\d\d\d)")
#\d,表示单个数字匹配,括号其分组作用
mynumber = "My number is 415-223-4567"
mo = phonenumber.search(mynumber)
print("phone: %s" % mo.group())
print("phone: %s" % mo.group(0))  
print("phone: %s" % mo.group(1))       
print("phone: %s" % mo.group(2))
# 打印结果显示如下:
# phone: 415-223-4567
# phone: 415-223-4567
# phone: 415
# phone: 223-4567
'''写法2'''
phonenumber = re.compile(r"(\d{1,3})-(\d{1,3})-(\d{1,3})") #" - 可自行更改,依据原数据为准!
# \d{1,3} 表示1到3个数字,既符合要求。其中括号其分组作用
mynumber = "My number is 415-223-4567"
mo = phonenumber.search(mynumber)
print("phone: %s" % mo.group())
print("phone: %s" % mo.group(0))  
print("phone: %s" % mo.group(1))       #利用括号分组
print("phone: %s" % mo.group(2))
print("phone: %s" % mo.group(3))
# 打印结果显示如下:
# phone: 415-223-456
# phone: 415-223-456
# phone: 415
# phone: 223
# phone: 456
'''写法3'''
phonenumber = re.compile(r"(\d+)-(\d+)-(\d+)") #" - 可自行更改,依据原数据为准!
# \d+ 表示后面有多少个数字,都符合要求。其中括号其分组作用
mynumber = "My number is 415-223-4567"
mo = phonenumber.search(mynumber)
print("phone: %s" % mo.group())
print("phone: %s" % mo.group(0))  
print("phone: %s" % mo.group(1))       #利用括号分组
print("phone: %s" % mo.group(2))
print("phone: %s" % mo.group(3))
# 打印结果显示如下:
# phone: 415-223-456
# phone: 415-223-456
# phone: 415
# phone: 223
# phone: 456

2、匹配对象实例(2):主要匹配/提取字符串中对应的数值

'''举例:1'''
data = "(【123,2132121]】】3123004231423"
'''匹配数字开头,数字结尾的数值,其中要是有括号或者其他字符,则以括号或者其他字符之前的数字为准'''
texts = re.findall(r"\d+\,\d+",data)
print("找出对应的数值:",texts)
#找出对应的数值: ['123,2132121']
'''举例:2'''
data = "123之后偶家搜查就2432jfds萨达--"
'''只匹配数字'''
texts = re.findall(r"\d+",data)
print("找出对应的数值:",texts) # texts为列表
#找出对应的数值(列表类型):['123,2132121']
'''举例:3'''
'''只匹配除了数字的字符,包括:字母,中文,特殊字符等'''
texts = re.findall(r"\D+",data)
print("找出对应的数值:",texts) # texts为列表
#找出对应的数值: ['之后偶家搜查就', 'jfds萨达']
'''举例:4'''
'''只匹配中文'''
texts = re.findall('[\u4e00-\u9fa5]',data)
print("找出对应的数值:",texts) # texts为列表
#找出对应的数值: ['之', '后', '偶', '家', '搜', '查', '就', '萨', '达']

3、运用正则表达式删除指定字符串:

number = ("a s ,s ,  3  a,qw   ,  + + d +  s -  nm")
result = re.split(r"[\s\,\+\-]+",number)
#简单理解就是正则表达式中表示的数字,空格,符号,是不会出现在分裂后的字符串列表中,也就是删除[\s\,\+\-]中的数字,空格,符号
print(result)

4、正则表达式匹配对象(可选)实例:

regexs = re.compile(r"Bat(man|tian|Fey|her|hello|tigner)")  #指定匹配对象
mo1 = "BatFey lost a whell Battian Bathello 1234 Battigner"
shuju = regexs.search(mo1)
print("shuju: %s" % shuju.group())   
#shuju: BatFey
#用问号实现可选匹配
batregex = re.compile(r"Bat(wo)?man")   #正则表达式中(wo)?部分表明,模式wo是可选的分组。
mo2 = batregex.search("The adventures of Batman")
print("\n问号实现可选匹配: %s" % mo2.group())
#问号实现可选匹配: Batman
mo3 = batregex.search("The adventures of Batwoman")
print("\n问号实现可选匹配: %s" % mo3.group())
#问号实现可选匹配: Batwoman
#用星号(*)实现可选匹配,意味着“匹配零次和多次”,即星号之前的分组。
batregex = re.compile(r"Bat(wo)*man")   #正则表达式中(wo)?部分表明,模式wo是可选的分组。
mo2 = batregex.search("The adventures of Batman")
print("\n星号实现可选匹配: %s" % mo2.group())
mo3 = batregex.search("The adventures of Batwoman")
print("\n星号实现可选匹配: %s" % mo3.group())
mo4 = batregex.search("The adventures of Batwowowowowoman") #可以识别
print("\n星号实现可选匹配: %s" % mo4.group())
#用点-星号匹配所有字符
namegrex = re.compile(r"First name:(.*) Last name:(.*)")
mo = namegrex.search("First name:ALL name Last name:GHJWER")
print("\n用点-星号匹配所有字符:%s" % mo.group())
#用点-星号匹配所有字符:First name:ALL name Last name: GHJWER
namegredy = re.compile(r"<.*>")     #贪心模式
shuzhi = namegredy.search("<hell0 world> for dinner.>")
print("\nshuzhi:%s" % shuzhi.group())
#shuzhi:<hell0 world> for dinner.>
namegredy = re.compile(r"<.*?>")     #非贪心模式
shuzhi = namegredy.search("<hell0 world> for dinner.>")
print("\nshuzhi:%s" % shuzhi.group())
#shuzhi:<hell0 world> for dinner.>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山中坐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值