正则表达式-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 准 ...
随机推荐
- Redis学习之路(003)- hiredis安装及测试
一. hiredis下载地址及C API github下载:https://github.com/redis/hiredis 安装脚本: #!/bin/zsh git clone https://g ...
- C语言连接MySQL数据库(转)
c++连接MySQL有两种方式,1是原始的方法,2是用 Connector c++ .Connector c++ 只是一种封装,使之更加方便. 1.原始方法 这里归纳了C API可使用的函数,并在下 ...
- Tensorflow 相关概念
一.概述 人工智能:artificial intelligence 权重: weights 偏差:biases 图中包含输入( input).塑形( reshape). Relu 层( Relulay ...
- cmake3.8X64编译opencv3.2出现opencv_ffmpeg、opencv_ffmpeg_64、ippicv_windows_20151201.zip无法下载问题解决方案
cmake版本:cmake3.8.0 开发环境:Visual Studio 2017 x64 解决方法:1.在opencv安装目录下sources\3rdparty\ffmpeg\ffmpeg.cma ...
- C# Httpclient客户端操作
原文地址:https://www.cnblogs.com/Xujg/p/4113387.html HttpClient 当前主流用法,异步请求,自.NET4.5开始可从Nuget包管理中获取. usi ...
- c# 中内部类的简单介绍 C#内部类
最近在看java一方面的书籍,看到一个很奇怪的问题,java类中还可以再定义一个类,这种结构非常特殊!后来才发现我知识浅薄了,原来C#中也有内部类,之前都一直没有注意过这个语法结构! 使用内部类有这样 ...
- process credentials(二)
一.前言 为什么要写一个关于进程如何创建的文档?其实用do_fork作为关键字进行索引,你会发现网上的相关文档数以万计.作为一个内核工程师,对进程以及进程相关的内容当然是非常感兴趣,但是网上的资料并不 ...
- 步进电机驱动器 和H桥
http://bbs.eeworld.com.cn/thread-489952-1-1.html
- 下载必要的android sdk
做android自动化测试,必须要下载anroid sdk 下载后最少安装方法:
- Matlab 2016b 正式版下载
Matlab 2016b 正式版下载 这两天为完成最优化大作业,zyy又开始鼓捣matlab了.之前我装的是matlab 2013a,发现不支持求解整数规划,遂更新了一下. 下载地址 链接:http ...