正则表达式——分组

( )

在正则表达式中,匹配字符时候,若有用括号分组的情况,则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']

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...