模式匹配与正则表达式

正则表达式

正则表达式,简称为 regex,是文本模式的的描述方法

Python 中所有正则表达式的函数都在 re 模块中。在交互式环境中输入以下代码,导入该模块
>>> import re

向 re.compile()传入一个字符串值,表示正则表达式,它将返回一个 Regex 模式对象(或者就简称为 Regex 对象)。

Regex 对象的 search()方法查找传入的字符串,寻找该正则表达式的所有匹配
如果字符串中没有找到该正则表达式模式,search()方法将返回 None。如果找到了该模式,search()方法将返回一个 Match 对象。Match 对象有一个 group()方法,它返回被查找字符串中实际匹配的文本

>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
>>> mo = phoneNumRegex.search('My number is 415-555-4242.') 
>>> print('Phone number found: ' + mo.group())
Phone number found: 415-555-4242

变量名 mo 是一个通用的名称,用于 Match 对象。这里,我们将期待的模式传递给 re.compile(),并将得到的 Regex 对象保存在phoneNumRegex 中

在这里插入图片描述

利用口号分组

添加括号将在正则表达式中创建“分组”:(\d\d\d)-(\d\d\d-\d\d\d\d)。然后可以使用 group()匹配对象方法,从一个分组中获取匹配的文本。
正则表达式字符串中的第一对括号是第 1 组。第二对括号是第 2 组。向 group()匹配对象方法传入整数 1 或 2,就可以取得匹配文本的不同部分。向 group()方法传入 0 或不传入参数,将返回整个匹配的文本

>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') 
>>> mo = phoneNumRegex.search('My number is 415-555-4242.') 
>>> mo.group(1)
'415'
>>> mo.group(2)
'555-4242'
>>> mo.group(0)
'415-555-4242' 
>>> mo.group()
'415-555-4242'

想要一次就获取所有的分组,请使用 groups()方法

>>> mo.groups()
('415', '555-4242')
>>> areaCode, mainNumber = mo.groups()
>>> print(areaCode)
415
>>> print(mainNumber)
555-4242

括号在正则表达式中有特殊的含义,但是如果你需要在文本中匹配括号,就需要用倒斜杠对(和)进行字符转义。

>>> phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
>>> mo = phoneNumRegex.search('My phone number is (415) 555-4242.')
>>> mo.group(1)
'(415)'
>>> mo.group(2)
'555-4242'

用管道匹配多个分组

字符‘|’称为“管道”。希望匹配许多表达式中的一个时,就可以使用它

>>> heroRegex = re.compile (r'Batman|Tina Fey')
>>> mo1 = heroRegex.search('Batman and Tina Fey.') 
>>> mo1.group()
'Batman'
>>> mo2 = heroRegex.search('Tina Fey and Batman.')
>>> mo2.group()
'Tina Fey'

如果 Batman 和 Tina Fey 都出现在被查找的字符串中,第一次出现的匹配文本,将作为 Match 对象返回

>>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
>>> mo = batRegex.search('Batmobile lost a wheel')
>>> mo.group()
'Batmobile'
>>> mo.group(1)
'mobile'

用问号实现可选匹配

有时候,想匹配的模式是可选的。就是说,不论这段文本在不在,正则表达式都会认为匹配。字符?表明它前面的分组在这个模式中是可选的

>>> batRegex = re.compile(r'Bat(wo)?man') 
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'
>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'

你可以认为?是在说,“匹配这个问号之前的分组零次或一次”。
如果需要匹配真正的问号字符,就使用转义字符?。

用星号匹配零次或多次

*(称为星号)意味着“匹配零次或多次”,即星号之前的分组,可以在文本中出现任意次

>>> batRegex = re.compile(r'Bat(wo)*man')
>>> mo1 = batRegex.search('The Adventures of Batman') 
>>> mo1.group()
'Batman'
>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'
>>> mo3 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo3.group()
'Batwowowowoman'

用加号匹配一次或多次

‘ * ’意味着“匹配零次或多次”,+(加号)则意味着“匹配一次或多次”。
加号前面的分组必须“至少出现一次”

>>> batRegex = re.compile(r'Bat(wo)+man') 
>>> mo1 = batRegex.search('The Adventures of Batwoman')
>>> mo1.group()
'Batwoman'
>>> mo2 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo2.group()
'Batwowowowoman'
>>> mo3 = batRegex.search('The Adventures of Batman') 
>>> mo3 == None
True

用花括号匹配特定次数

如果想要一个分组重复特定次数,就在正则表达式中该分组的后面,跟上花括号包围的数字。例如,正则表达式(Ha){3}将匹配字符串’HaHaHa’,但不会匹配’HaHa’,因为后者只重复了(Ha)分组两次。

除了一个数字,还可以指定一个范围,即在花括号中写下一个最小值、一个逗号和一个最大值。例如,正则表达式(Ha){3,5}将匹配’HaHaHa’、‘HaHaHaHa’和’HaHaHaHaHa’。
也可以不写花括号中的第一个或第二个数字,不限定最小值或最大值。
例如(Ha){3,}将匹配 3 次或更多次实例,(Ha){,5}将匹配 0 到 5 次实例

通配字符

在正则表达式中,.(句点)字符称为“通配符”。它匹配除了换行之外的所有字符。

>>> atRegex = re.compile(r'.at')
>>> atRegex.findall('The cat in the hat sat on the flat mat.')
['cat', 'hat', 'sat', 'lat', 'mat'] 

可以用点-星(.*)表示“任意文本”。

>>> nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)')
>>> mo = nameRegex.search('First Name: Al Last Name: Sweigart')
>>> mo.group(1)
'Al'
>>> mo.group(2)
'Sweigart'

点-星使用“贪心”模式:它总是匹配尽可能多的文本。要用“非贪心”模式匹配所有文本,就使用点-星和问号。

>>> nongreedyRegex = re.compile(r'<.*?>')
>>> mo = nongreedyRegex.search('<To serve man> for dinner.>')
>>> mo.group()
'<To serve man>'
>>> greedyRegex = re.compile(r'<.*>')
>>> mo = greedyRegex.search('<To serve man> for dinner.>')
>>> mo.group()
'<To serve man> for dinner.>'

用句点字符匹配换行
点-星将匹配除换行外的所有字符。通过传入 re.DOTALL 作为 re.compile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符。

>>> noNewlineRegex = re.compile('.*')
>>> noNewlineRegex.search('Serve the public trust.\nProtect the innocent. 
\nUphold the law.').group()
'Serve the public trust.'
>>> newlineRegex = re.compile('.*', re.DOTALL)
>>> newlineRegex.search('Serve the public trust.\nProtect the innocent.
\nUphold the law.').group()
'Serve the public trust.\nProtect the innocent.\nUphold the law.' 

贪心与非贪心匹配

Python 的正则表达式默认是“贪心”的,这表示在有二义的情况下,它们会尽可能匹配最长的字符串。花括号的“非贪心”版本匹配尽可能最短的字符串,即在结束的花括号后跟着一个问号

>>> greedyHaRegex = re.compile(r'(Ha){3,5}')
>>> mo1 = greedyHaRegex.search('HaHaHaHaHa')
>>> mo1.group()
'HaHaHaHaHa'
>>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?')
>>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa')
>>> mo2.group() 
'HaHaHa'

findall()方法

Regex对象也有一个findall()方法。search()将返回一个Match对象,包含被查找字符串中的“第一次”匹配的文本,而 findall()方法将返回一组字符串,包含被查找字符串中的所有匹配。

 >>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups 
>>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
['415-555-9999', '212-555-0000'] 

字符分类

在这里插入图片描述在这里插入图片描述

>>> xmasRegex = re.compile(r'\d+\s\w+')
>>> xmasRegex.findall('12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7
swans, 6 geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge') 
['12 drummers', '11 pipers', '10 lords', '9 ladies', '8 maids', '7 swans', '6
geese', '5 rings', '4 birds', '3 hens', '2 doves', '1 partridge']

建立自己的字符分类

你可以用方括号定义自己的字符分类。例如,字符分类[aeiouAEIOU]将匹配所有元音字符,不论大小写

>>> vowelRegex = re.compile(r'[aeiouAEIOU]')
>>> vowelRegex.findall('RoboCop eats baby food. BABY FOOD.') 
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

请注意,在方括号内,普通的正则表达式符号不会被解释。这意味着,你不需要前面加上倒斜杠转义.、*、?或()字符。例如,字符分类将匹配数字 0 到 5 和一个句点。你不需要将它写成[0-5.]。

通过在字符分类的左方括号后加上一个插入字符(^),就可以得到“非字符类”。非字符类将匹配不在这个字符类中的所有字符。

>>> consonantRegex = re.compile(r'[^aeiouAEIOU]')
>>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')
['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
', 'B', 'B', 'Y', ' ', 'F', 'D', '.'] 

插入字符和美元字符

在正则表达式的开始处使用插入符号(^),表明匹配必须发生在被查找文本开始处。
正则表达式的末尾加上美元符号( ) , 表 示 该 字 符 串 = = 必 须 以 这 个 正 则 表 达 式 的 模 式 结 束 = = 以 同 时 使 用 和 ),表示该字符串==必须以这个正则表达式的模式结束== 以同时使用^和 ====使,表明整个字符串必须匹配该模式,也就是说,只匹配该字符串的某个子集是不够的

>>> beginsWithHello = re.compile(r'^Hello') 
>>> beginsWithHello.search('Hello world!')
<_sre.SRE_Match object; span=(0, 5), match='Hello'>
>>> beginsWithHello.search('He said hello.') == None
True
>>> endsWithNumber = re.compile(r'\d$')
>>> endsWithNumber.search('Your number is 42')
<_sre.SRE_Match object; span=(16, 17), match='2'>
>>> endsWithNumber.search('Your number is forty two.') == None
True
>>> wholeStringIsNum = re.compile(r'^\d+$') 
>>> wholeStringIsNum.search('1234567890')
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
>>> wholeStringIsNum.search('12345xyz67890') == None
True
>>> wholeStringIsNum.search('12 34567890') == None
True

不区分大小写的匹配

通常,正则表达式用你指定的大小写匹配文本。
要让正则表达式不区分大小写,可以向 re.compile()传入 re.IGNORECASE 或 re.I,作为第二个参数。

>>> robocop = re.compile(r'robocop', re.I)
>>> robocop.search('RoboCop is part man, part machine, all cop.').group()
'RoboCop'
>>> robocop.search('ROBOCOP protects the innocent.').group() 
'ROBOCOP'
>>> robocop.search('Al, why does your programming book talk about robocop so much?').group() 
'robocop'

sub()方法替换字符串

Regex对象的 sub()方法需要传入两个参数。第一个参数是一个字符串,用于取代发现的匹配。第二个参数是一个字符串,即正则表达式。sub()方法返回替换完成后的字符串。

>>> namesRegex = re.compile(r'Agent \w+')
>>> namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
'CENSORED gave the secret documents to CENSORED.'

在 sub()的第一个参数中,可以输入\1、\2、\3……。表示“在替换中输入分组 1、2、3……的文本”。

例如,假定想要隐去密探的姓名,只显示他们姓名的第一个字母。要做到这一点,可以使用正则表达式 Agent (\w)\w*,传入 r’\1****'作为 sub()的第一个参数。字符串中的\1 将由分组 1 匹配的文本所替代,也就是正则表达式的(\w)分组

>>> agentNamesRegex = re.compile(r'Agent (\w)\w*') 
>>> agentNamesRegex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent 
Eve knew Agent Bob was a double agent.')
A**** told C**** that E**** knew B**** was a double agent.'

管理复杂的正则表达式
匹配复杂的文本模式,可能需要长的、费解的正则表达式。你可以告诉 re.compile(),忽略正则表达式字符
串中的空白符和注释
可以向 re.compile()传入变量 re.VERBOSE,作为第二个参数。
可以将正则表达式放在多行中,并加上注释,像这样:

phoneRegex = re.compile(r'''(
 (\d{3}|\(\d{3}\))?           # area code
 (\s|-|\.)?                   # separator
 \d{3}                        # first 3 digits
 (\s|-|\.)                    # separator
 \d{4}                        # last 4 digits
 (\s*(ext|x|ext.)\s*\d{2,5})? # extension
 )''', re.VERBOSE)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LabVIEW是一种可视化编程环境,用于控制、测量和数据采集等应用。LabVIEW的匹配模式(Match Pattern)和匹配正则表达式函数(Match Regular Expression)都是用于字符串匹配的工具,它们之间有以下的异同: 相同点: 1. 字符串匹配:两者都用于在一个字符串中匹配指定的模式。 2. 正则表达式:两者都支持使用正则表达式进行匹配,正则表达式可以实现更复杂的匹配模式。 3. 返回结果:两者都返回匹配结果,可以用于判断是否匹配成功,并获取匹配到的内容。 不同点: 1. 使用方式不同:匹配模式是通过设置输入字符串、模式字符串和其他一些参数来进行匹配。而匹配正则表达式函数则需要设置输入字符串和正则表达式,并通过输出一个布尔值来表示是否匹配成功。 2. 功能不同:匹配模式可以匹配字符串中的指定模式部分,但不能提取匹配的内容;而匹配正则表达式函数不仅支持匹配,还可以提取匹配到的内容。 3. 匹配精度不同:匹配模式是基于简单的字符串匹配,不支持使用正则表达式的高级匹配规则。而匹配正则表达式函数可以使用正则表达式的各种高级规则,能更精确地进行匹配。 综上所述,LabVIEW的匹配模式和匹配正则表达式函数在使用方式、功能和匹配精度等方面存在一些差异。选择使用哪个函数取决于具体的匹配需求以及对匹配精度的要求。如果仅需进行简单的字符串匹配,可以使用匹配模式;而如果需要更复杂的匹配规则以及提取匹配内容的功能,则需要使用匹配正则表达式函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值