爬虫常用正则表达式

  • 单字符
    • . : 除换行以外所有字符
    • [] : [aoe] [a-w] 匹配集合中任意一个字符
    • \d:元字符,代表0-9中的任意一个
    • \D : 代表任意一个非数字字符
    • \w : 数字、字母、下划线、中文
    • \W : 非\w
    • \s : 所有的空白字符,包括空格、制表符、换页符等,等价于\f\n\r\t\v
    • \S : 非空白
  • 数量修饰
    • * : 任意多次 >=0
    • +: 至少一次 >=1
    • ? : 可有可无 0次或一次
    • {m} : 固定m次 hello{3,}
    • {m,} : 至少m次
    • {m,n} : m-n次
  • 边界
    • $ : 以某某结尾
    • ^ : 以某某开头
  • 分组
    • (ab)
  • 贪婪模式 : .*
  • 非贪婪(惰性)模式 : .*?
  • re.I : 忽略大小写
  • re.M : 多行匹配
  • re.S : 但行匹配
  • re.sub : (正则表达式,替换内容,字符串)
import re

key = "javapython1myslqpython1"

print(re.findall('python1', key)[1])

key = "<html><h1>hello world<h1></html>"
print(re.findall('<h1>(.*)<h1>', key))

string = "I like 170 girl"
print(re.findall('\d', string))

key = "http://www.baidu.com and https://www.shaoshaossm.github.io"
print(re.findall('https://', key))

key = 'am@shao.com'
print(re.findall('s.*?\.', key))

key = 'saas and asa and saaas'
print(re.findall('sa{1,2}s',key))


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值