import re
line = "Cats are smarter than dogs"
# .* 表示任意匹配除换行符(\n、\r)之外的任何单个或多个字符
matchObj = re.match(r'(.*) (.*) (.*)', line, re.M | re.I)
print("matchObj.group() : ", matchObj.group())
print("matchObj.group(1) : ", matchObj.group(1))
print("matchObj.group(2) : ", matchObj.group(2))
print("\n")
matchObj = re.match(r'(.*)are(.*) (.*)', line, re.M | re.I)
print("matchObj.group() : ", matchObj.group())
print("matchObj.group(1) : ", matchObj.group(1))
print("matchObj.group(2) : ", matchObj.group(2))
#r'表示这个字符串 转义字符不转义
# print(r"**\nerw")
# ptint("**\nnerw")
#group分组
#group()是所有分组合起来
#group(1)是第一个分组
#分组取决于(.*)与(.*)中间的字符:
#如果(.*) (.*) //中间有空格
#那么分组就是按照空格分
#(.*)和(.*?)的区别:
#(.*?) 能够匹配上的最小的分组
#(.*)是最大的分组:
#例子:
#line = "Cats are smarter than dogs"
#matchObj = re.match(r'(.*) (.*) (.*)', line, re.M | re.I)
#分组是Cats are smarter 和 than 和 dogs
#line = "Cats are smarter than dogs"
#matchObj = re.match(r'(.*) (.*)(.*)', line, re.M | re.I)
#分组是Cats are smarter than 和dogs
#groups()相当于把group用下标变成组
字符串分割
print(re.split('(\W+)', ' runoob, runoob, runoob.'))
# ['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob', '.', '']
print(re.split('(\W+)', ' runoob, runoob, runoob'))
# ['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob']
#首尾有没有标点