正则表达式

正则常用规则:

一张图足以说明一切,哈哈哈!!

方法:

  • find()查找

  • findall()查找所有内容

  • replace()替换

  • split()分割

  • search()全局匹配

  • match()从开始的位置匹配

  • sub()也是替换,比replace简单

  • complie()声明一个正则,方便后面使用

修饰符:

  • re.I 使匹配对大小写敏感

  • re.M 多行匹配

  • re.S 使.匹配换行在内的所有字符 (最常使用)

  • re.U 根据Unicode字符集解析字符

实例:

import re

s='hello world'

print(s.find('ll'))#查找ll

ret=s.replace('ll','xx')#替换ll

print(ret)

print(s.split(' '))#更具空格分割

ret=re.findall('w\w{2}l','hello world')

print(ret)#匹配出worl,返回的是一个列表

ret=re.findall('w.l','hello w\tld')#.(通配符)表示匹配一个字符,除了\n

print(ret)#匹配出w\tl

ret=re.findall('h...o','dsadashello')

print(ret)#匹配出hello

#元字符$ 在结尾匹配

ret=re.findall('h...o$','dsadashello')

print(ret)#匹配出hello,必须以o结尾

#*:重复匹配 0到无穷

#ret=re.findall('h.*o','sdsdfgfhellojbcvbtr')

#print(ret)#匹配出hello 贪婪匹配,尽可能多的去匹配,你可以在结尾加上个o试试会不会是另一个结果)

#+:重复匹配 1 到无穷

ret=re.findall('h.+o','sdsdfgfhojbcvbtr')

print(ret)#没有匹配到内容

#?: [0,1]

ret=re.findall('h.?o','sdsdfgfhooojbcvbtr')

print(ret)#匹配除hoo

#{}: 自己定义

ret=re.findall('a{5,10}b','aaaaaaaaaab')

print(ret)

#[]: 字符集[a-z],可以取消元字符的特殊功能\ ^ -除外

ret=re.findall('a[a,b,c]x','acx')

print(ret)#[a,b,c]中间只匹配一个字符

ret=re.findall('[.,*,$,,]','acx.sd*fg$tyt,')

print(ret)

#[^t]除了t的所有

ret=re.findall('[^ts,,5]','ts456t,tt')

print(ret)

findall():所有结果返回在一个列表里

search():返回一个对象(object),对象需要调用group()

match():只在字符的开始匹配,返回的也是一个对象,对象需要调用group()

split():分割

======

ret=re.split('[s,k,d]','dsajk')

print(ret)#从s,k,d分割

======

ret=re.sub('a..x','s','hfalaxss')

print(ret)#将匹配到的字符串换成s,也是替换

=======

ret=re.compile('\.com')声明一个正则,便于后面调用

obj=ret.findall('dsasds.comgfdhgd')

print(obj)#匹配出.com

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值