Python的 re 模块:核心函数和方
1. 以 compile()函数编译正则表达式
导入 re 模块后,在compile()
中编译正则表达式,例如:pattern =
re.compile(‘正则表达式’,re.S),然后便可以动用pattern来进行匹配了。
以compile
中还得带动模块属性,即re.S,re.I,re.L,re.M,re.X等。
2.郎才女貌对象和 group()和 groups()方法
匹配对象有零星独至关重要的道:group()和
groups()。 调用 match()或者
search()返回的目标就是相当对象,group()要么返回整个匹配对象,要么因要求回特定子组。
groups()则单纯返回一个分包唯一要全部子组的元组。如果无子组的求,那么当group()仍然返回整个匹配时,groups()
返回一个空元组。
3. 运用 match()方法匹配字符串
match()
函数计算打字符串的开场部分对模式进行匹配。如果配合成功,就归一个匹配对象;如果匹配失败,就回
None,匹配对象的 group()方法能够用于展示大成功的配合。
1 html = '<html><head>title</head><body>2333</body></html>'
2 pattern = re.compile('<.*?head>(.*?)<.*?body>(.*?)<.*?>')
3
4 m = re.match(pattern, html)
5
6 print(m.group())
7 print(m.group(1))
8 print((m.group(2)))
9 print(m.groups())
10 # 输出:
11 # <html><head>title</head><body>2333</body>
12 # title
13 # 2333
14 # ('title', '2333')
>>> re.match('foo', 'food on the table').group()
'foo'
4. 用到 search()在一个字符串中寻找模式,(搜索)
search()的做事章程与
match()完全一致,不同之处在于
search()会因此它的字符串参数,在肆意位置对加正则表达式模式寻找第一次于出现的相当情况。
如果搜索到成功的配合,就会回来一个配合对象;否则,返回
None。
search() 和 match()
不同之处在于,search() 会搜索字符串中间部分。
>>> m = re.match('foo', 'seafood') # 匹配失败
>>> m = re.search('foo', 'seafood') # 使用 search() 代替
>>> if m is not None: m.group()
...
'foo' # 搜索成功,但是匹配失败,在seafood中搜索foo
5.重复、特殊字符以及分组
用一个配合电子邮件地址的正则表达式做例子。(\w+@\w+\.com),这个正则表达式只能够匹配简单的地址。
为了以域名前补加主机名称支持,例如
www.xxx.com,需要采用?,\w+@(\w+\.)?\w+\.com,让(\w+\.)可选。
>>> pattern = '\w+@(\w+\.)?\w+\.com'
>>> re.match(pattern, 'nobody@xxx.com').group()
'nobody@xxx.com'
>>> re.match(pattern, 'nobody@www.xxx.com').group()
'nobody@www.xxx.com'
进一步扩展该示例,允许擅自数量的中子域名在。把?改呢
* 号。\w+@(\w+\.)*\w+\.com
>>> patt = '\w+@(\w+\.)*\w+\.com'
>>> re.match(patt, 'nobody@www.xxx.yyy.zzz.com').group()
'nobody@www.xxx.yyy.zzz.com'
使用圆括声泪俱下来配合与保存子组,以便为继续处理。
>>> m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
>>> m.group() # 完整匹配
'abc-123'
>>> m.group(1) # 子组 1
'abc'
>>> m.group(2) # 子组 2
'123'
>>> m.groups() # 全部子组
('abc', '123')
group()通常用于因寻常方式展示有的配合有,但也克用来获取
取各个匹配的子组。可以使
groups()方法来赢得一个含有有匹配子字符串的元组。
6.运用 sub()和 subn()搜索和替换
有星星点点独函数/方法用于落实搜索和替换功能:sub()和
subn()。两者几乎千篇一律,都是用有字符串中装有匹配正则表达式的有些进行某种形式的更迭。
用来替换的局部常见是一个字符串,
但它呢恐怕是一个函数,该函数返回一个据此来替换的字符串。
subn()和 sub()的不同点是subn()
还回来一个表示替换的总数,替换后的字符串和象征替换总数的数字一起当一个怀有两独因素的元组返回。
>>> re.sub('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
'attn: Mr. Smith\012\012Dear Mr. Smith,\012'
>>>
>>> re.subn('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
('attn: Mr. Smith\012\012Dear Mr. Smith,\012', 2)
>>>
>>> print(re.sub('X', 'Mr. Smith', 'attn: X\n\nDear X,\n'))
attn: Mr. Smith
Dear Mr. Smith,
>>> re.sub('[ae]', 'X', 'abcdef')
'XbcdXf'
>>> re.subn('[ae]', 'X', 'abcdef')
('XbcdXf', 2)
7.扩大符号
通过行使 (?iLmsux)
系列选,用户可以直接当正则表达式里面指定一个要基本上个号,而休是通过
compile()或者其他 re 模块函数。
下面为局部利用 re.I/IGNORECASE
的演示皇冠直营现金网开户, 最后一个演示在 re.M/MULTILINE 实现多实施混合:
>>> re.findall(r'(?i)yes', 'yes? Yes. YES!!') # (?i) 不区分大小写
['yes', 'Yes', 'YES']
>>> re.findall(r'(?i)th\w+', 'The quickest way is through this tunnel.') ['The', 'through', 'this']
>>> re.findall(r'(?im)(^th[\w ]+)', """
... This line is the first,
... another line,
... that line, it's the best
... """)
['This line is the first', 'that line']