正则表达式-python-无捕获分组与分支选择
无捕获分组
当你要将一部分规则作为一个整体对它进行某些操作,比如指定其重复次数时,你需要将这部分规则用
(?:)
把它包围起来。
分支条件
在正则表达式中,分支条件是一个很常用的条件。
满足条件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-无捕获分组与分支选择的更多相关文章
- 前向否定界定符 python正则表达式不匹配某个字符串 以及无捕获组和命名组(转)
[编辑] 无捕获组和命名组 精心设计的 REs 也许会用很多组,既可以捕获感兴趣的子串,又可以分组和结构化 RE 本身.在复杂的 REs 里,追踪组号变得困难.有两个功能可以对这个问题有所帮助.它们也 ...
- Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间
Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间 一丶URLconf配置 ...
- excel支持正则表达式提取字符函数(支持RegExp捕获分组)
一.要让excel脚本支持Microsoft VBScript Regular Expressions 5.5 ,按快捷键alt+F11,出现下图界面,操作如图示: 二.添加VBA代码: 代码添加完毕 ...
- Python中正则匹配使用findall,捕获分组(xxx)和非捕获分组(?:xxx)的差异
转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例 ...
- Python 正则表达式 (python网络爬虫)
昨天 2018 年 01 月 31 日,农历腊月十五日.20:00 左右,152 年一遇的月全食.血月.蓝月将今晚呈现空中,虽然没有看到蓝月亮,血月.月全食也是勉强可以了,还是可以想像一下一瓶蓝月亮洗 ...
- 正则表达式-Python实现
1.概述: Regular Expression.缩写regex,regexp,R等: 正则表达式是文本处理极为重要的工具.用它可以对字符串按照某种规则进行检索,替换. Shell编程和高级编程语言中 ...
- split与re.split/捕获分组和非捕获分组/startswith和endswith和fnmatch/finditer 笔记
split()对字符串进行划分: >>> a = 'a b c d' >>> a.split(' ') ['a', 'b', 'c', 'd'] 复杂一些可以使用r ...
- Django路由系统---url无命名分组
django重点之url无命名分组[参数有顺序要求] settigs.py:增加STATICFILES_DIRS静态资源路径配置,名称为创建的文件夹名称 'DIRS': [os.path.join(B ...
- 结合Wireshark捕获分组深入理解TCP/IP协议栈
摘要: 本文剖析了浏览器输入URL到整个页面显示的整个过程,以百度首页为例,结合Wireshark俘获分组进行详细分析整个过程,从而更好地了解TCP/IP协议栈. 一.俘获分组 1.1 准 ...
随机推荐
- 【SQL】SQL中Case When的用法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...
- codesmith连接postgresql修复bug
转:CodeSmith7代码生成器针对PostgreSQL数据库无法使用的Bug修复全过程 我自己又修改过,完整的PostgreSQLSchemaProvider.cs文件如下 using Npgsq ...
- IDEA使用笔记(三)——小齿轮的显示和隐藏(Autoscroll from Source)
在玩快捷键的时候,不清楚自己操作了什么,突然间发现——能直接定位到当前可编辑文件的哪个小齿轮,不见了,找了一会也没弄出来,从网上搜索吧!也没看到对应的方法,后来自己耐下心来复盘自己的操作,终于发现了, ...
- 【Algorithm】自顶向下的归并排序
一. 算法描述 自顶向下的归并排序:采用分治法进行自顶向下的程序设计方式,分治法的核心思想就是分解.求解.合并. 先将长度为N的无序序列分割平均分割为两段 然后分别对前半段进行归并排序.后半段进行归并 ...
- win7 git 安装
下载git: https://git-scm.com/download/win 二.Git安装 去官网下载完后一路下一步完成安装,如下图: 安装完后先在系统环境变量中看下是否配置 然后在桌面右 ...
- Oracle 12C -- native left outer join的加强
在11g中SQL> select count(*) 2 from emp a, dept b, bonus c 3 where a.deptno(+) = b.deptno 4 ...
- 一段js代码
原文地址 [].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math ...
- php性能优化(一)压力測试工具篇
ab使用 Apache附带的压力測试工具ab,很easy使用,而且全然能够摸你各种条件对Webserver发起測试请求.ab能够直接在Webserver本地发起測试请求.这对于须要了解server的处 ...
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
<Connector port="8070" protocol="HTTP/1.1" connectionTimeout="20000" ...
- 设置eclipse/myeclipse的智能提示
打开eclipse/myeclipse选择 window-->Preferences-->JAVA-->Editor-->单击Content Assist–>在右边找到A ...