正则表达式—search,match,findall

# search 字符串中包含的就行

# match 必须第一位就匹配成功 否则失败 就相当于 search(‘^’)

# findall 匹配所有 返回列表

# search 字符串中包含就行
# match 必须第一位就匹配成功 否则失败 就相当于search('^')
# findall 匹配所有 返回列表
print(re.search('\d', '123abc'))
print(re.match('\d', '123abc'))

<re.Match object; span=(0, 1), match='1'>
<re.Match object; span=(0, 1), match='1'>

print(re.search('\d', 'abc123'))
print(re.match('\d', 'abc123'))
print(re.search('^\d', 'abc123'))

<re.Match object; span=(3, 4), match='1'>
None
None

print(re.match('\d+', '123abc'))
print(re.match('\d*', '123abc'))
print(re.match('\d*', 'abc'))
print(re.match('\d+', 'abc')

<re.Match object; span=(0, 3), match='123'>
<re.Match object; span=(0, 3), match='123'>
<re.Match object; span=(0, 0), match=''>
None
      
print(re.findall('\d', '1234567asd789'))
['1', '2', '3', '4', '5', '6', '7', '8', '9']
print(re.findall('\d', 'abc'))
[]
print(re.findall('\d{2}', '123456asd789'))
['12', '34', '56', '78']
      
myStr = '<div>我是HTML标签</div><div>div标签</div><div></div>'
# 匹配除div标签中的内容
print(re.findall('<div>.*?</div>', myStr)) # 拒绝贪婪
print(re.findall('<div>.*</div>', myStr)) # 贪婪

['<div>我是HTML标签</div>', '<div>div标签</div>', '<div></div>']
['<div>我是HTML标签</div><div>div标签</div><div></div>']
     
print(re.findall('<div>.+?</div>', myStr))
print(re.findall('<div>.+</div>', myStr))

['<div>我是HTML标签</div>', '<div>div标签</div>']
['<div>我是HTML标签</div><div>div标签</div><div></div>']

# 存储括号中的值
print(re.findall('<div>(.*?)</div>', myStr))
print(re.findall('<div>(.*)</div>', myStr))
      
['我是HTML标签', 'div标签', '']
['我是HTML标签</div><div>div标签</div><div>']
      
print(re.findall('(<div>(.*?)</div>)', myStr))
[('<div>我是HTML标签</div>', '我是HTML标签'), ('<div>div标签</div>', 'div标签'), ('<div></div>', '')]