import re
# ˆ:以......开头
text = 'hello world'
rem = re.search('^hello',text )
print(rem.group())
# $:以......结尾
text = 'hello world'
rem = re.search('world$',text )
print(rem.group())
# |:匹配多个字符串或者表达式
text = 'hello world'
rem = re.search('^hello|world$',text )
print(rem.group())
# 贪婪和非贪婪
text = '12345'
rem = re.search('\d+?',text )
print(rem.group())
# 案例1:提取html标签名称
text = '<h1>这是标题<h1>'
rem = re.search('<.+?>', text )
print(rem.group())
# 案例2:验证一个字符是不是0~100之间的数字
# 0, 1, 99, 100
text = '01' # 100、1000
rem = re.match('0$|[1-9]\d?$|100$', text )
print(rem.group())
市场调查与分析-正则表达式—开始、结束、贪婪和非贪婪
于 2021-01-21 16:00:43 首次发布