首页 技术 正文
技术 2022年11月12日
0 收藏 874 点赞 2,507 浏览 824 个字
  • \n:表示第 n 个捕获:

    >> s = "<html><h1>what the fuck!</h1></html>"
    >> p = r"<(.+)><(.+)>(.+)</\2></\1>"
    # \2 对应第二个捕获,也即 h1,则 </\2> 为:</h1>
    # \1 对应第一个捕获,也即 html,则 </\1> 为:</html>
    >> re.match(p, s).group(3)
    'what the fuck!'

1. 匹配邮箱与html标签

  • 匹配邮箱:

    >> mail = 'zch921005@126.com'
    >> reg = r"(\w{4,20})@(126|qq|gmail|163|outlook)\.(com)"
    # 正则表达式中不要出现无意义的空格
    >> re.match(reg, mail).group(1)
    'zch921005'
    >> re.match(reg, mail).group(2)
    '126'
    >>
  • 匹配 html 标签:

    >> s='<div><a href="https://support.google.com/chrome/?p=ui_hotword_search" rel="external nofollow"  rel="external nofollow" target="_blank">更多</a><p>dfsl</p></div>'
    >> re.search(r'<a.*>(.*)</a>', s).group(1)
    '更多'

2. 起别名

https://blog.csdn.net/HeatDeath/article/details/70171569

>>> s = '<html><h1>what the fuck!</h1></html>'
>>> p = r"<(?P<key1>.+)><(?P<key2>.+)>(.+)</(?P=key2)></(?P=key1)>"
>> re.match(p, s).group(1)
'html'
>> re.match(p, s).group(2)
'h1'
>> re.match(p, s).group(3)
'what the fuck!'
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918