正则表达式re

 

一、re模块的group和groups

      group 和 groups 是两个不同的函数

      m.group()== m.group(0) == 返回所有匹配到的字符

      m.group(N) 返回第N组括号匹配到的字符

 

     m.groups() 返回所有括号匹配到的字符,以元组格式存储

     m.groups() = (m.group(1),m.group(2),....)

 

    测试数据:

    

line = "Cats are smarter than dogs"

matchObj = re.match(r‘(.*) are (.*?) .*‘,line,re.M | re.I)

if matchObj:
print("matchObj.group: ",matchObj.group)
print("matchObj.group() : ",matchObj.group())
print("matchObj.group() : ",matchObj.group(0))
print("matchObj.group(1) : ",matchObj.group(1))
print("matchObj.group(2) : ",matchObj.group(2))
else:
print("No match!!")

print(matchObj.groups())
print(matchObj.groups()[0])
print(matchObj.groups()[1])


测试结果:

  matchObj.group: <built-in method group of _sre.SRE_Match object at 0x10f5ffe00>  matchObj.group() : Cats are smarter than dogs  matchObj.group() : Cats are smarter than dogs  matchObj.group(1) : Cats  matchObj.group(2) : smarter  (‘Cats‘,‘smarter‘)  Cats  smarter

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) &lt;script ...