文章目录
正则表达式:
- 正则表达式,英文名为Regular Expression,又称规则表达式。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
- 在主流操作系统(nix[Linux, Unix等]、Windows、HP、BeOS等)、主流的开发语言(delphi、Scala、PHP、C#、Java、C++、Objective-c、Swift、VB、Javascript、Ruby以及Python等)、数以亿万计的各种应用软件中,都可以看到正则表达式优美的舞姿。***
Python正则表达式: - Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。re 模块使 Python 语言拥有全部的正则表达式功能。同时,re 模块是用c语言写的,其匹配速度非常快。
- 其中compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。re模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。
re模块:
re.match方法:
-
re.match
尝试从字符串的起始位置匹配一个规则,匹配成功就返回match对象,否则返回None。可以使用group()获取匹配成功的字符串。 -
语法:re.match(pattern, string, flags=0)
-
参数说明:
示例1(无标志位):
>>> import re
>>> str = 'Python is the best language in the world’
# match 只能匹配以xxx开头的子符串,第一个参数是正则,第二个参数是需要匹配的字符串
>>> res = re.match('P',str)
>>> res.group() # 匹配成功使用group方法取出字符串
'P’
>>>
示例2(有标志位)
>>> import re
>>> str = 'Python is the best language in the world’
>>> res = re.match(‘p’,str,re.I)
>>> res.group()
'P'
- 如果同时使用多个标志位使用|分割,比如re.I | re.M
- flags可选标志位
我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式
示例:
>>> str = 'Python is the best language in the world’
>>> res = re.match('(.*) is (.*?) .*',str)
>>> res
<_sre.SRE_Match object; span=(0, 40), match='Python is the best language in the world’>
>>> res.group()
'Python is the best language in the world’
>>> res.group(1)
'Python’
>>> res.group(2)
'the’
常用的匹配规则-匹配字符:
常用的匹配规则-匹配字符
.(点): 匹配任意除换行符之外的字符
>>> import re
>>> str = 'aaaa’
>>> re.match('.',str).group()
'a’
>>> re.match('..',str).group()
'aa’
>>>
[ ] :匹配中括号内的任意一个字符
>>> str = 'hello’
>>> re.match('[he]',str).group() 'h’
>>> str = 'Hello’
>>> re.match('[He]',str).group() 'H’
>>> str = 'eee’
>>> re.match('[He]',str).group() 'e’
>>>
\d :匹配一个数字
>>> str = '124asb’
>>> re.match('\d',str).group()
'1’
>>> re.match('\d\d',str).group()
'12’
>>>
\D 匹配非数字,即不是数字
>>> str = 'a124asb’
>>> re.match('\D',str).group()
'a’
>>> str = '124asb' # 数字开头匹配错误
>>> re.match('\D'