2 27re.py

那是個好男孩發表於2019-02-27
"""
匹配目標
"""
# 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()
"""