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.>