首页 > 分享 > 正则表达式(regex)实现模式匹配

正则表达式(regex)实现模式匹配

正则表达式(regex)实现模式匹配

使用过程:

调入regex所在模块创建regex对象查询打印调用

import re #re为正则表达式模块 phoneNumberRegex=re.compile(r'ddd-dddd') #phoneNumberRegex为创建的正则表达式 mo=phoneNumberRegex.search('My number is 415-9683.') #mo用来保存匹配结果 print('phone number found:'+mo.group()) #group调出打印 1234

phone number found:415-9683 1 d表示数字 , ‘d’实际应输入’\d’ , 从而避免当成转义字符翻译(用来打印符号)。字符串首个引号之前加入 r , 该字符串可标记为原始字符串,不会自动翻译转义字符。
r’ddd’ 等同于 ‘\d\d\d’

功能:

括号创建分组

phoneNumberRegex=re.compile(r'(dddd-)(ddddddd)') mo=phoneNumberRegex.search('my number is 0551-6306943') 12

mo.group() #打印所有分组 1

'0551-6306943' 1

mo.group(0) #打印所有分组 1

'0551-6306943' 1

mo.group(1) #第一组从0开始计数,不是0 1

'0551-' 1

mo.group(2) 1

'6306943' 1

分组后mo得到多个值的元组,可用于赋值

example1,example2=mo.groups() #注意是 .groups() 是个复数 12

example1 1

'0551-' 1

example2 1

'6306943' 1

查找 (415) 5555-444 如何创建正则表达式?(如何处理括号?)

phoneNumberRegex=re.compile(r'((ddd)) (dddd-ddd)') 1 用管道匹配多个值

'|'表示管道(或),匹配多个表达式中的第一个

heroregex=re.compile(r'Batman | Tina Fey') mo=heroregex.search('Batman and Tina Fey') mo.group() 123

'Batman ' 1

管道与括号实现找前缀

batregex=re.compile(r'Bat(man|mobile|copter|woman)') mo=batregex.search('Batmobile lost a wheel.') 12

mo.group() 1

'Batmobile' 1

mo.group(1) 1

'mobile' 1 用 ?实现可选匹配

?表示可选内容出现0此或1次

batmanregex=re.compile(r'Bat(wo)?man') mo1=batmanregex.search('the adventure of Batman') mo1.group() 123

'Batman' 1

mo2=batmanregex.search('the adventures of Batwoman.') mo2.group() 12

'Batwoman' 1 用 * 实现匹配0次或多次

batregex=re.compile(r'bat(wo)*man') 1

mo1=batregex.search('the adventures of batman') mo1.group() 12

'batman' 1

mo2=batregex.search('the adventures of batwowowowowoman') mo2.group() 12

'batwowowowowoman' 1 用 + 实现匹配1次或多次

batregex=re.compile(r'bat(wo)+man') 1 用花括号 {} 匹配特定次数

(ha){3} 匹配于 hahaha

(ha){,3}匹配于ha出现0-3次

(ha){5,}匹配于ha出现5次及以上

贪心匹配与非贪心匹配

贪心匹配:找最长字符串
非贪心匹配:找最短字符串
Python的正则表达式默认贪心匹配: (a){3,5}默认匹配5次
非贪心匹配修正:(a){3,5}? 默认匹配最短

用 .findall() 方法查找所有匹配

.findall()返回的是字符串列表

字符分类

d 等效于正则表达式 (0|1|2|3|4|5|6|7|8|9) 等效于 [0-9]

缩写字符分类表示d0到9的任何数字D除0到9的数字以外的任何字符w任何字母、数字或下划线字符(可以认为是匹配“单词”字符)W除字母、数字和下划线以外的任何字符s空格、制表符或换行符(可以认为是匹配“空白”字符)S除空格、制表符和换行符以外的任何字符 建立 [] 自己的字符分类

vowelregex=re.compile(r'[aeiouAEIOU]') vowelregex.findall('roboccop eats baby food.BABY FOOD.') 12

['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O'] 1 用 ^ 表示非对应字符

vowelregex=re.compile(r'[^aeiouAEIOU]') vowelregex.findall('roboccop eats baby food.BABY FOOD.') 12

['r','b','c','c','p',' ','t', 's', ' ', 'b', 'b','y',' ','f','d','.','B','B','Y',' ','F','D','.'] 1 用 ^ 字符表示必须匹配字符串的 起始处

hello=re.compile(r'^hello') hello.search('he said hello.') == None 123

True 1 用 $ 字符表示必须匹配字符串的结束处

end=re.compile(r'end$') end.search('ending is a new begining') == None 12

True 1

e1=end.search('this never dsadend') e1.group() 12

'end' 1 用 . 字符匹配除换行以外所有字符(单个) 用 .* 匹配任意字符串

nameregex=re.compile(r'First Name: (.*) Last Name: (.*) ') mo=nameregex.search('First Name: AI Last Name: Sweidau ') mo.group() 123

'First Name: AI Last Name: Sweidau ' 1

这是贪心匹配,非贪心策略使用(.*?)

用 . 匹配换行符(re.DOTALL)

import re nonewllineregex=re.compile('.*') nonewllineregex.search('Serve the public trust.nProtect the innocent.nUphold the law.').group() 123

'Serve the public trust.' 1

nonewllineregex=re.compile('.*',re.DOTALL) nonewllineregex.search('Serve the public trust.nProtect the innocent.nUphold the law.').group() 12

'Serve the public trust.nProtect the innocent.nUphold the law.' 1 不区分大小写的匹配

使用re.compile()传入re.IGNORECASE或re.I作为第二个参数

import re robocop=re.compile(r'robocop',re.I) 12 用sub()方法替换字符串

nameregex=re.compile(r'Agent w+') nameregex.sub('CENSORED','Agent Alice gave the secret documents to Agent BOb') 12

'CENSORED gave the secret documents to CENSORED' 1

使用re.VERBOSE作为第二参数忽略正则表达式中的空格与换行

搭配三重引号’’'使用,创造一个多行的字符串。三重引号要接大括号

phone_number_regex1 = re.compile(r'''( (d{4}|(d{4})? #area code may includes() (s|-)? #separator (d{7}) #7 digits )''', re.VERBOSE) 12345 用 .join() 方法使用元素链接成新的字符串

join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串

for groups in phone_number_regex1.findall(text): phone_number1 = '-'.join([groups[1],groups[3]]) maches.append(phone_number1) 123

相关知识

【学习笔记】JavaScript 寻找字串的方法:includes/indexOf/search/match
查找和替换字符类中的花括号
bnmjstu
手机号码的正则表达式是什么?
地名地址匹配算法研究
简单的查询语法
完整 Lucene 查询语法的示例
查询字符串的通用语法规则
Java查询Mysql数据库时,查询条件带有反斜线\的处理方式
判断花括号是否匹配

网址: 正则表达式(regex)实现模式匹配 https://m.huajiangbk.com/newsview106046.html

所属分类:花卉
上一篇: 成功解决报错Cannot fin
下一篇: 《C语言点滴》第1章 程序猿&&