市场调查与分析-正则表达式—开始、结束、贪婪和非贪婪

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())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

juicy-hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值