Python3正则表达式:match函数

import re

url = "www.baidu.com"

# 匹配包含www的字符串
re_compile = re.compile("www")

# match从起始位置开始匹配,且只匹配一次,如果不没找到,则返回None
match = re_compile.match(url)

# .span用来获取匹配到的字符串所对应的下标位置
#re_compile.match(url).span()

# match返回的是match对象,获取值使用group
print(match.group())

import re

line = "Cats are smarter than dogs"
# .* 表示任意匹配除换行符(\n、\r)之外的任何单个或多个字符
matchObj = re.match(r'(.*) are (.*) .*', line, re.M | re.I)

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


 

D:\Python37\python.exe D:/Python-study/reTest.py
www
matchObj.group() :  Cats are smarter than dogs
matchObj.group(1) :  Cats
matchObj.group(2) :  smarter than

Process finished with exit code 0

 

你可能感兴趣的:(python,python正则表达式,python,match,match)