python regex,Python 正则表达式(regex)

Python 正则表达式(regex)

正则表达式

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑

正则表达式非Python独有,在Python中使用re模块实现

常见匹配模式

模式 描述

\w 匹配数字、字母、下划线

\W 匹配非数字、字母、下划线

\s 匹配任意空白字符,等价于[\t\n\r\f]

\S 匹配任意非空字符

\d 匹配任意数字,等价于[0-9]

\D 匹配任意非数字

\A 匹配字符串开始

\Z 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串

\z 匹配字符串结束

\G 匹配最后匹配完成的位置

\n 匹配一个换行符

\t 匹配一个制表符

^ 匹配字符串的开头

$ 匹配字符串的末尾

. 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。

[...] 用来表示一组字符,单独列出:[abc]匹配"a","b"或"c"

[^...] 不再[]中的字符:[^abc]匹配除了a,b,c之外的字符

* 匹配0个或多个的表达式

+ 匹配1个或多个的表达式

? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪模式

{n} 精确匹配n个前面表达式

{n,m} 匹配n到m次由前面的正则表达式定义的片段,贪婪模式

a|b 匹配a或b

() 匹配括号内的表达式,也表示一个组

re.match

re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话, match()就返回none

re.match(pattern,string,flags=0)

常规匹配

import re

content = 'Hello 111 2222 World hello python'

print(len(content))

res = re.match('^Hello\s\d\d\d\s\d{4}\s\w{5}\s.*python$', content)

print(res)

print(res.group())

print(res.span())

运行结果:

33

Hello 111 2222 World

(0, 21)

泛匹配

import re

content = 'Hello 111 2222 World hello python'

res = re.match('^Hello.*python$', content)

print(res)

print(res.group())

print(res.span())

运行结果:

Hello 111 2222 World hello python

(0, 33)

匹配目标

import re

content = 'Hello 111 2222 World hello python'

res = re.match('^Hello\s(\d+)\s(\d+)\s.*python$', content)

print(res)

print(res.group(1), res.group(2))

运行结果:

111 2222

贪婪模式

import re

content = 'Hello 111 2222 World hello python'

res = re.match('^H.*(\d+)\s(\d+).*python$', content)

print(res)

print(res.group(1), res.group(2))

运行结果:

1 2222

非贪婪模式

import re

content = 'Hello 111222 World hello python'

res = re.match('^He.*?(\d+).*?python$', content)

print(res)

print(res.group(1))

运行结果:

111222

匹配模式

模式 描述

re.I 匹配的字符忽略大小写

re.M 多行匹配

re.L 本地化识别匹配

re.U 根据Unicode进行相应化解析

re.S 让 . 匹配包括换行符

import re

content = """Hello 1112222 World

hello python"""

res = re.match('^H.*?(\d+).*?python$', content, re.S)

print(res)

print(res.group(1))

运行结果:

1112222

转义

import re

content = """The apple's price is $5.00"""

res = re.match('The apple\'s price is \$5.00', content, re.S)

print(res)

print(res.group())

The apple's price is $5.00

总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、由换行符就用re.S

re.search

re.search 扫描整个字符串并返回第一个成功的匹配

# 使用re.match()

import re

content = """This is a string"""

res = re.match('a', content, re.S)

print(res)

运行结果:

None

# 使用re.search()

import re

content = """This is a string"""

res = re.search('a\s\w*', content, re.S)

print(res)

print(res.group())

运行结果:

a string

总结:为匹配方便,能用search就不用match

re.findall

搜索字符串,以列表形式返回全部能匹配的子串

import re

content = """This is a string"""

res = re.findall('a\s\w*', content, re.S)

print(res)

运行结果:

['a string']

re.sub

替换字符串中每一个匹配的子串后返回替换后的字符串

import re

content = """This is 222211111 string"""

res = re.sub('\d+', 'a',content)

print(res)

运行结果:

This is a string

re.compile

将正则字符串编译成正则表达式对象

将一个正则表达式串编译成正则对象,以便于复用该匹配模式

import re

content = """This is 222211111 string"""

pattern = re.compile('\d+')

res = re.search(pattern, content)

print(res)

print(res.group())

运行结果:

222211111

欢迎访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值