2 27re.py
""" 匹配目标 """ # import re # content = 'Hello 123 4567 World_This is a Regex Demo' # result = re.match("^Hello\s(\d+)\sWorld$", content) # print(result.group(1)) """ 通用匹配 """ # import re # content = 'Hello 123 4567 World_This is a Regex Demo' # result = re.match("^Hello.*Demo$", content) # print(result) """ 贪婪与非贪婪 """ # import re # content = 'Hello 123 4567 World_This is a Regex Demo' # # result = re.match("^He.*?(\d+).*Demo$", content) # result = re.match("^He.*(\d+).*Demo$", content) # print(result) # print(result.group(1)) #group()和group(0)效果一样 # import re # content = "http://weibo.com/comment/kEraCN" # result1 = re.match("^http.*?comment/(.*?)", content) # result2 = re.match("^http.*?comment/(.*?)$", content) # result3 = re.match("^http.*?comment/(.*)", content) # result4 = re.match("^http.*?comment/(.*)$", content) # print(result1.group(1)) # print(result2.group(1)) # print(result3.group(1)) # print(result4.group(1)) """ 修饰符 """ # re.I re.S """ 转义匹配 """ # / """ search() """ # import re # content = "http://weibo.com/comment/kEraCN" # result = re.search("tp.*?comment/(.*?)$", content) # print(result.group(1)) """ findall() sub() compile() """
爱笑的男孩运气都不会差