正则表达式

正则表达式

1.正则表达式概述

查找符合复杂规则的字符串的需要

匹配或者查找符合某些规则的字符串

概念:记录文本规则的代码

0\d{2}-\d{8} 就是一个正则表达式,匹配座机号码(其中/d代表匹配0-9,{N}表示对数量的限制)

特点:语法可读性差,通用性强,适用于多种编程语言

2.re模块的介绍

使用正则表达式需要导入re模块

#导入模块
import re
#match匹配,这里只是创建了结果对象,不能直接提取
result=re.match(#正则表达式,#要匹配的字符串)
#提取数据
result.group()

实例

import re
result=re.match("itc","itcast")
info=result.group()
print(info)
3.匹配单个字符

目的:使用re模块匹配单个字符

代码功能
.匹配任意一个字符除了/n
[]匹配[ ]中列举的字符1
\d匹配数字,0-9
\D匹配非数字
\s匹配空白,即空格,tab键
\S匹配非空白
\w匹配非特殊字符,即a-z,A-Z,0-9
\W匹配特殊字符,即非字母,非数字,非汉字

实例演示:

import re

#匹配数据
result=re.match("itcast.","itcast@")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")


#匹配数据,注意不需要,
result=re.match("itcast[01234]","itcast4")
result=re.match("itcast[0-9]","itcast4")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\d","itcast4")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\D","itcast@")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\s","itcast ")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\S","itcast0")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\w","itcast测")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")

#匹配数据,注意不需要,
result=re.match("itcast\W","itcast&")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("没有匹配到")
4.匹配多个字符
代码功能
*匹配前一个字符出现从0次或无限次,即可有可无
+匹配前一个字符出现从1次或无限次,即至少有1次
匹配前一个字符出现从1次或0次,即要么有一次,要么一次没有
{m}匹配前一个字符出现从m次
{m,n}匹配前一个字符出现从m到n次

案例

#匹配多个字符
#导入模块
import re
#匹配数据
result=re.match("itcast\d*itcast","itcast3343itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match("itcast\d+itcast","itcast5itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match("itcast\d?itcast","itcast3itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match("itcast\d{4}itcast","itcast3333itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match("itcast\d{4,6}itcast","itcast33333itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")
5.匹配开头和结尾
代码功能
^匹配字符串开头
&匹配字符串结尾
[^指定字符]匹配除了指定字符以外的所有字符

^放在第一位,修饰其后边的,$放在最后一位,修饰其前边的

案例

import re
#匹配开头结尾

#匹配数据
result=re.match("^\d.{0,}","3itcast")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match(".*\d$","itcast3")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")


#匹配数据
result=re.match("^\d.*\d$","3itcast3")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")

#匹配数据
result=re.match(".*[^4]$","itcast3")
#提取数据
if result:
    info=result.group()
    print(info)
else:
    print("未匹配到")
6.匹配分组
代码功能
|匹配左右任意一个表达式
(ab)将括号中字符作为一个分组
\num引用分组num匹配到的字符串
(?P)分组起别名
(?P=name)引用别名为name分组匹配到的字符串

案例

#匹配分组
import re
#1.在列表中["apple","banana","orange","pear"],匹配apple和pear
frui=["apple","banana","orange","pear"]
#通过for循环获取字符串数据
for value in frui:
    result=re.match("apple|pear",value)
    #判断是否成功
    if result:
        #提取数据
        info=result.group()
        print(info)
    else:
        print("未匹配到")

#匹配出163,126,qq等邮箱
#\转义字符的应用
result=re.match("[0-9a-zA-Z]{4,20}@(163|126|qq)\.com","hello@qq.com")
info=result.group()
print(info)

#匹配qq号码,要求提取出qq,qq的数字号码
result=re.match("(qq):([1-9]\d{4,11})","qq:123456789")
#group里边无填充(本质为0)代表的是匹配到的所有数据,如果有1,则代表匹配到的第一个分组,以此类推
info=result.group(0)
print(info)
info1=result.group(1)
print(info1)
info2=result.group(2)
print(info2)


#匹配出<html>hh</html>
#两个\\返回最本身的意思,才能利用第一个正则表达式分组的规则
result=re.match("<(html)>.*</\\1>","<html>hh</html>")
info=result.group()
print(info)

#匹配出<html><h1>www.baidu.com</h1></html>
result=re.match("<(html)><(h1)>.*</\\2></\\1>","<html><h1>www.baidu.com</h1></html>")
info=result.group()
print(info)

#匹配出<html><h1>www.baidu.com</h1></html>
result=re.match("<(?P<name1>html)><(?P<name2>h1)>.*</(?P=name2)></(?P=name1)>","<html><h1>www.baidu.com</h1></html>")
info=result.group()
print(info)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值