Python 基础知识(十一)-----正则表达式

一. 正则表达式

1. 概述

  1. 概念
    (1)Regular Expression
    (2) 一种文本模式,描述在搜索文本时要匹配一个或多个字符串
  2. 典型场景
    (1)数据验证
    (2)文本扫描
    (3)文本提取
    (4)文本替换
    (5)文本分割
  3. 语法
    (1)字面值:普通字符,需转义(,^,$…)
    (2)元字符
  4. 匹配
    (1)单字,预定义元字符:. 除/n外所有字符,\d数字,\D 所有非数字, \s 空白字符,\S 非空白字符,\w 字母数字字符,\W 非字线数字
    (2)批量备选:| yes|no
    (3) 量词(字符、元字符,字符集如何重复):?0或1次,* 0或多次,+1或多次,特定{}, {n} n次,{n,}至少n次,{,m}最多m次
    (3)贪婪或非贪婪:贪婪(尽量匹配最大的范围),非贪婪(尽量匹配最小范围,*后加?)
    (4)边界匹配:^行首,$行尾,\b单词边界

2. Python 正则

1.模块
(1)import re

import re
text = 'Tom is 8 years old. Mike is 25 years old.'
pattern = re.compile('\d+')
pattern.findall(text)
['8', '25']
re.findall('\d',text)
['8', '2', '5']
  1. RegexDbject
    (1) 模式对象,表现编译后的正则表达式
    (2)编译: re.compile(r’模式’)
s = '\\author:Tom'
pattern = re.compile('\\author')
pattern.findall(s)
[]
pattern = re.compile('\\\\author')
pattern.findall(s)
['\\author']
pattern = re.compile(r'\\author')
pattern.findall(s)
['\\author']

(3)findall():查找所有非重叠匹配,返回list

text = 'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
pattern = re.compile(r'\d+')
pattern.findall(text)
['8', '23', '87']
p_name = re.compile(r'[A-Z]\w+')
p_name.findall(text)
['Tom', 'Mike', 'Peter']

(4).match(string[,pos[,endpos]]):匹配,仅从起始位置, 返回 HatchObject

text = '<html><head></head><body></body></html>'
pattern.match(text)
<_sre.SRE_Match object; span=(0, 6), match='<html>'>
text2 = ' <html><head></head><body></body></html>'
pattern.match(text2)
pattern.match(text2,1)
<_sre.SRE_Match object; span=(1, 7), match='<html>'>

(5).search(string[,pos[,endpos]]): 任意位置搜索,返回 HatchObject

text = 'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
p1 = re.compile(r'\d')
p2 = re.compile(r'[A-Z]\w+')
p1.match(text)
p2.match(text)
<_sre.SRE_Match object; span=(0, 3), match='Tom'>
p1.search(text)
<_sre.SRE_Match object; span=(7, 8), match='8'>

(6) .finditer(): 返回可迭代对象,返回 HatchObject

text
'Tom is 8 years old. Mike is 23 years old. Peter is 87 years old.'
p1
re.compile('\\d')
p1.findall(text)
['8', '2', '3', '8', '7']
it = p1.finditer(text)
for m in it:
..</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值