正则表达式——分组
发布时间:2020-12-14 00:57:26 所属栏目:百科 来源:网络整理
导读:( ) 在正则表达式中,匹配字符时候,若有用括号分组的情况,则findall函数会优先返回分组中匹配的内容,如一下代码显示: #!/usr/bin/python #coding:utf8 import reres = r"(hello) world" res1 = r"(hello|hi) world" res2 = r"hello world" s = "hello wo
( )在正则表达式中,匹配字符时候,若有用括号分组的情况,则findall函数会优先返回分组中匹配的内容,如一下代码显示: #!/usr/bin/python
#coding:utf8
import re
res = r"(hello) world"
res1 = r"(hello|hi) world"
res2 = r"hello world"
s = "hello world hi world"
print re.findall(res,s) #显示hello
print "#"*20
print re.findall(res1,s) #显示hello或者hi
print "#"*20
print re.findall(res2,s) #显示 hello world
$ python e8.py
['hello']
####################
['hello','hi']
####################
['hello world']
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |