( )
在正则表达式中,匹配字符时候,若有用括号分组的情况,则findall函数会优先返回分组中匹配的内容,如一下代码显示:
#!/usr/bin/python@H_403_7@
#coding:utf8@H_403_7@
import@H_403_7@ re
res = r"(hello) world"@H_403_7@
res1 = r"(hello|hi) world"@H_403_7@
res2 = r"hello world"@H_403_7@
s = "hello world hi world"@H_403_7@
print@H_403_7@ re.findall(res,s) #显示hello@H_403_7@
print@H_403_7@ "#"@H_403_7@*20@H_403_7@
print@H_403_7@ re.findall(res1,s) #显示hello或者hi@H_403_7@
print@H_403_7@ "#"@H_403_7@*20@H_403_7@
print@H_403_7@ re.findall(res2,s) #显示 hello world@H_403_7@
$ python e8.py
['hello'@H_403_7@]
####################@H_403_7@
['hello'@H_403_7@,'hi'@H_403_7@]
####################@H_403_7@
['hello world'@H_403_7@]