python笔记-正则表达式

一、正则表达式

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

###以下是看学习视频示例中的笔记###

 

 

import re

###匹配单个字符
#1、re.match 只可以匹配对象开头部分,如果匹配不到就会出错。匹配完成后返回的是一个re.MATCH的对象,需要调用group()才能把这个对象的内容打出来,返回类型是str
# text ='hello'
# he_match = re.match('he',text)
# print(he_match.group())

#2、点(.)能匹配任意符,只能匹配一个字符。但是不匹配换行符。
# text ='hello'
# he_match = re.match('...',text)
# print(he_match.group())

#3、 \d:只能匹配数字(0-9)
# text ='123'
# he_match = re.match('\d',text)
# print(he_match.group())

#4、 \D:匹配任意的非数字
# text ='sdfas'
# he_match = re.match('\D',text)
# print(he_match.group())

#5、 \s:匹配空白字符(\n \t \r 空格)
# text ='\nad '
# he_match = re.match('\s',text)
# print(he_match.group())

#6、 \w:匹配a-z 和 A-Z以及数字和下划线
# text ='z'
# he_match = re.match('\w',text)
# print(he_match.group())

#7、 \W:匹配正好和\w相反
# text ='a'
# he_match = re.match('\W',text)
# print(he_match.group())

#8、 []:组合的方式,只要满足中括号中的字符,就可以匹配。如果需要匹配多个字符,就在中括号后面加上“+”,就能匹配多个字符了。
# text ='0731-888888'
# he_match = re.match('[\d\-]+',text)
# print(he_match.group())

#8.1中括号的形式代替\d
# text ='0731-888888'
# he_match = re.match('[0-9]',text)
# print(he_match.group())

#8.2中括号的形式代替\D,"^"代表着“非”
# text ='0731-888888'
# he_match = re.match('[^0-9]',text)
# print(he_match.group())

# #8.3中括号的形式代替\w
# text ='_'
# he_match = re.match('[a-zA-Z0-9]',text)
# print(he_match.group())

# #8.4中括号的形式代替\W,加个“^”,代表着非
# text ='_'
# he_match = re.match('[a-zA-Z0-9]',text)
# print(he_match.group())

###匹配多个字符
#9、  *:可以匹配0个或者任意多个字符.,match 表示字符串前面部分的字符
# text ='0755adad'
# he_match = re.match('\s*',text)
# print(he_match.group())

#10. +:匹配1个或者多个字符
# text ='+0755a-dad'
# he_match = re.match('\+',text)
# print(he_match.group()

#11. ?:匹配1个或者0个字符
# text ='+adad'
# he_match = re.match('\w?',text)
# print(he_match.group())

#12. {m}:匹配m个字符
# text ='adad'
# he_match = re.match('\w{2}',text)
# print(he_match.group())

#13. {m,n}:匹配m-n个字符
# text ='adaddfds'
# he_match = re.match('\w{2,7}',text)
# print(he_match.group())

#14、"^":表示以...开始
#match,只查找字符头部位置一次。search表示在字符串里面搜索到匹配的字符
# text ='adaddfds'
# he_match = re.search('s',text)
# print(he_match.group())

#14、$:表示以...结尾
# text ='xxxx@163.com'
# he_match = re.match('\w+@163.com$',text)
# print(he_match.group())

#15、贪婪模式和非贪婪模式:默认是贪婪模式。非贪婪模式在“+”“*”“.”的后面加“?”表示为非贪婪模式
# text ='0123456'
# he_match = re.match('\d+?',text)
# print(he_match.group())

#16、匹配0-100之间的数字
#可以出现1,2,3,4,10,100
#有三种情况:1,99,100
#不可以出现09,101
# text ='1010'
# he_match = re.match('[1-9]\d?$|100$',text)
# print(he_match.group())


######正则表达式的函数#########
#match、search、group其实整个正则表达式就是一个大的分组
# text= "apple's price $99,orange's price is $10"
# ret  =re.search('.*(\$\d+).*(\$\d+)',text)
# print(ret.group())
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(1,2))  #等同于ret.groups()

#查找函数:fineall():
# text= "apple's price $99,orange's price is $10"
# ret_list = re.findall('\\$\d+',text)
# print(ret_list)

#替换函数:sub():
# text= "apple's price $99,orange's price is $10"
# ret_list = re.sub('\\$\d+','0',text)  ##替换成0
# print(ret_list)

#分割:split 函数:
# text="hello&is world in nihao "
# ret = re.split('[^a-zA-Z]',text)
# print(ret)

#compile:对于一些经常要用的到的正则表达式,可以使用compile进行编译,后期再使用的时候就可以直接拿出来用,
# 只醒效率会更快。而且compile还可以指定flag = re.VERBOSE,再写正则表达式的是很好可以做好注释。
# text= "the number is 20.50"
# r = re.compile(r"\d+\.?\d*")
# ret = re.search(r,text)
# print(ret.group())


#######小案例#########
#1、验证手机号码
# text ='13320121354'
# he_match = re.match('1[345678]\d{9}',text)
# print(he_match.group())

#2、验证邮箱
# text ='3600121_29@q163.com'
# he_match = re.match('\w+@[a-z0-9]+\.[a-z]+',text)
# print(he_match.group())

#3、验证URL ."|"表示与的意思,需要用小括号包裹起来,“+”表示,
# text ='https://www.baidu.com'
# he_match = re.match('(http|https|ftp)://[^\s]+',text)
# print(he_match.group())

#4、验证身份证
# text ='44090219950111563X'
# he_match = re.match('\d{17}[\d|x|X]',text)
# print(he_match.group())


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

危机时刻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值