万能的正则表达式

  • 2019 年 10 月 6 日
  • 筆記

首先,正则表达式是干什么用的? 正则表达式是用来匹配字符串的,用来实现字符串的检索,替换,匹配验证等 有了它,提取我们想要的信息就变得很容易了

我们来看一下正则常用规则:

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

方法:

  • 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('ww{2}l','hello world')

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

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

print(ret)#匹配出wtl

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