无捕获分组

当你要将一部分规则作为一个整体对它进行某些操作,比如指定其重复次数时,你需要将这部分规则用

(?:)

把它包围起来。

分支条件

在正则表达式中,分支条件是一个很常用的条件。

满足条件A 或者 满足条件B  ,这个时候我们就可以使用分支条件了。

分支条件使用的符号为

       |

代码示例:

我们突然发现,它把字符串分割成两个部分了

I have a dog  和  cat    而不是    I have a dog  和   I have a cat

如果我们只要区分dog和cat呢?正则要怎么写?我添加一个括号试试

还是不对,前面的 “I have a ”根本没有匹配

正确的写法是应该使用无捕获分组

来个高级一点的

import requests

import re

url = "https://docs.djangoproject.com/en/1.6/intro/tutorial02/"

res = requests.get(url)

html = res.content

# 捕获html中的 href 和 src 后的内容
lines =  re.findall("(?:(?<=href=\")|(?<=src=\"))(?:(?:ht|f)tp[s]?)?.*?(?=\")",html,re.S) for line in lines: print line

匹配结果如下:

root@kali:/recall/code# python re.py

/s/css/base.de56b042ddc0.css
/s/css/print.ac134bbb8dfc.css
/s/css/docs/docs.0047ef2d621d.css
/s/css/pygments.0d57d48be058.css
https://www.djangoproject.com/
/s/img/site/hdr_logo.b19c5e60269d.gif
https://www.djangoproject.com/
https://www.djangoproject.com/download/
https://docs.djangoproject.com/en/1.6/
https://www.djangoproject.com/weblog/
https://www.djangoproject.com/community/
https://code.djangoproject.com/
/en/1.6/
/en/1.2/intro/tutorial02/
/en/1.3/intro/tutorial02/
/en/1.4/intro/tutorial02/
/en/1.5/intro/tutorial02/
/en/1.7/intro/tutorial02/
/en/dev/intro/tutorial02/
#writing-your-first-django-app-part-2
../tutorial01/
#start-the-development-server
http://127.0.0.1:8000/admin/
../../_images/admin01.png
../../topics/i18n/translation/
#enter-the-admin-site
../../topics/auth/default/#topics-auth-creating-superusers
../../_images/admin02t.png
../../topics/auth/#module-django.contrib.auth
#make-the-poll-app-modifiable-in-the-admin
#explore-the-free-admin-functionality
../../_images/admin03t.png
../../_images/admin04t.png
../../_images/admin05t.png
../../ref/models/fields/#django.db.models.DateTimeField
../../ref/models/fields/#django.db.models.CharField
../../ref/models/fields/#django.db.models.DateTimeField
../../ref/settings/#std:setting-TIME_ZONE
../../_images/admin06t.png
#customize-the-admin-form
../../_images/admin07.png
../../_images/admin08t.png
../../_images/admin09.png
#adding-related-objects
../../_images/admin10.png
../../ref/models/fields/#django.db.models.ForeignKey
../../_images/admin11t.png
../../_images/admin15t.png
../../_images/admin12t.png
#customize-the-admin-change-list
../../_images/admin04t.png
../../_images/admin13t.png
../../_images/admin14t.png
../../ref/models/fields/#django.db.models.DateTimeField
#customize-the-admin-look-and-feel
#customizing-your-project-s-templates
../../ref/settings/#std:setting-TEMPLATE_DIRS
../../ref/settings/#std:setting-TEMPLATE_DIRS
#customizing-your-application-s-templates
../../ref/settings/#std:setting-TEMPLATE_DIRS
../../ref/templates/api/#template-loaders
#customize-the-admin-index-page
../../ref/settings/#std:setting-INSTALLED_APPS
../tutorial03/
../tutorial01/
../tutorial03/
/en/1.6/faq/
http://groups.google.com/group/django-users/
http://groups.google.com/group/django-users/
irc://irc.freenode.net/
http://django-irc-logs.com/
https://code.djangoproject.com/newticket?component=Documentation
#
#start-the-development-server
#enter-the-admin-site
#make-the-poll-app-modifiable-in-the-admin
#explore-the-free-admin-functionality
#customize-the-admin-form
#adding-related-objects
#customize-the-admin-change-list
#customize-the-admin-look-and-feel
#customizing-your-project-s-templates
#customizing-your-application-s-templates
#customize-the-admin-index-page
../tutorial01/
../tutorial03/
/en/1.6/contents/
/en/1.6/genindex/
/en/1.6/py-modindex/
/en/1.6/
../
/m/docs/django-docs-1.6-en.zip
http://media.readthedocs.org/pdf/django/1.6.x/django.pdf
http://media.readthedocs.org/epub/django/1.6.x/django.epub
http://readthedocs.org/
https://www.djangoproject.com/foundation/
https://www.djangoproject.com/trademarks/
http://mediatemple.net/
//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js

 

欢迎拍砖。

正则表达式-python-无捕获分组与分支选择的更多相关文章

  1. 前向否定界定符 python正则表达式不匹配某个字符串 以及无捕获组和命名组(转)

    [编辑] 无捕获组和命名组 精心设计的 REs 也许会用很多组,既可以捕获感兴趣的子串,又可以分组和结构化 RE 本身.在复杂的 REs 里,追踪组号变得困难.有两个功能可以对这个问题有所帮助.它们也 ...

  2. Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间

    Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间 一丶URLconf配置 ...

  3. excel支持正则表达式提取字符函数(支持RegExp捕获分组)

    一.要让excel脚本支持Microsoft VBScript Regular Expressions 5.5 ,按快捷键alt+F11,出现下图界面,操作如图示: 二.添加VBA代码: 代码添加完毕 ...

  4. Python中正则匹配使用findall,捕获分组(xxx)和非捕获分组(?:xxx)的差异

    转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例 ...

  5. Python 正则表达式 (python网络爬虫)

    昨天 2018 年 01 月 31 日,农历腊月十五日.20:00 左右,152 年一遇的月全食.血月.蓝月将今晚呈现空中,虽然没有看到蓝月亮,血月.月全食也是勉强可以了,还是可以想像一下一瓶蓝月亮洗 ...

  6. 正则表达式-Python实现

    1.概述: Regular Expression.缩写regex,regexp,R等: 正则表达式是文本处理极为重要的工具.用它可以对字符串按照某种规则进行检索,替换. Shell编程和高级编程语言中 ...

  7. split与re.split/捕获分组和非捕获分组/startswith和endswith和fnmatch/finditer 笔记

    split()对字符串进行划分: >>> a = 'a b c d' >>> a.split(' ') ['a', 'b', 'c', 'd'] 复杂一些可以使用r ...

  8. Django路由系统---url无命名分组

    django重点之url无命名分组[参数有顺序要求] settigs.py:增加STATICFILES_DIRS静态资源路径配置,名称为创建的文件夹名称 'DIRS': [os.path.join(B ...

  9. 结合Wireshark捕获分组深入理解TCP/IP协议栈

    摘要:     本文剖析了浏览器输入URL到整个页面显示的整个过程,以百度首页为例,结合Wireshark俘获分组进行详细分析整个过程,从而更好地了解TCP/IP协议栈.   一.俘获分组 1.1 准 ...

随机推荐

  1. 解析form表单数据

    //解析form表单数据 function parseFormData(params) { var args = new Object(); for(var key in params){ if(!p ...

  2. io分析神器blktrace

    一.概述 [许久之前就用过blktrace,现整理如下] 从linux 一个完整的IO入手分析: 一个I/O请求进入block layer之后,可能会经历下面的过程: Remap: 可能被DM(Dev ...

  3. Docker 构建Hadoop环境

    参考如下文章: Docker安装Hadoop Docker在本地搭建Hadoop分布式集群 Docker快速搭建Hadoop测试环境 从0开始用docker搭建 hadoop分布式环境 Docker- ...

  4. 解决『Manifest merger failed with multiple errors, see 』

    Error:Execution failed for task ':app:processDebugManifest'.> Manifest merger failed with multipl ...

  5. JQuery EasyUI combobox 省市两级联动

    表名:province  结构如下 CallIn.tpl 模板页 <select id="consult_province" name="consult_provi ...

  6. Win 7 IE11不能下载文件,右键另存为也不行

    在IE11中不能下载文件,右键另存为也无效. 发现 在IE11中点击“INTERNET选项”后,IE临时文件夹的地址没有显示,大小为0,修改只能让设置在8-8MB,注销再登录后,一切设置无效. 问题就 ...

  7. c# 笔试题及参考答案大全

    http://blog.csdn.net/smileberry1/article/details/3958670

  8. Proxy源代码分析——谈谈如何学习Linux网络编程

    Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...

  9. python groupby 函数 as_index

    在官方网站中对as_index有以下介绍: as_index : boolean, default True For aggregated output, return object with gro ...

  10. Linux系统和工具集

    Linux系统和工具集 阿里源 http://mirrors.aliyun.com/ http://centos.ustc.edu.cn/ 第三方包管理器 不同的发行版使用不同的软件包管理器,Cent ...