# -*- codeing = utf-8 -*-
# coding=gbk
# @Autor:ggui20
import re
if __name__ == '__main__':
# ^:以...开头:
# text = "hello world"
# result = re.search("world",text)
# print(result.group())
# $:以...结尾:
# text = "hello world"
# result = re.search("hello$",text)
# print(result.group())
# text = ""
# result = re.search("^$",text)
# print(result.group())
# |:匹配多个字符串或者表达式:
# 贪婪和非贪婪:
# text = "12345"
# #result = re.search("\d.*?",text)
# result = re.search("\d+?", text)
# print(result.group())
# 案例1:提取html标签名称:
# group()/group(0):匹配整个分组
# group(1):匹配第一个分组
# group(2):匹配第二个分组
# groups():获取所有的分组
text = "<h1>这是标题</h1>"
# result = re.search("<.+?>",text)
result = re.search("<h1>(.*?)</h1>", text)
print(result.group(1))
s = re.findall('<h1>(.*?)</h1>', text)
print(s[0])
# 案例2:验证一个字符是不是0-100之间的数字:
# 0,1,99,100
# 01
# text = "101"
# result = re.match("0$|[1-9]\d?$|100$", text)
# print(result.group())
14-开始结束和非贪婪
最新推荐文章于 2024-11-02 16:28:26 发布