博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
正则表达式-python-无捕获分组与分支选择
阅读量:7110 次
发布时间:2019-06-28

本文共 3832 字,大约阅读时间需要 12 分钟。

无捕获分组

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

(?:)

把它包围起来。

分支条件

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

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

分支条件使用的符号为 

|

 

 

代码示例:

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

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

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

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

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

 

 

来个高级一点的

import requestsimport reurl = "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

 

欢迎拍砖。

 

转载地址:http://rfghl.baihongyu.com/

你可能感兴趣的文章
Ubuntu Server 18.04 通过 nvm 安装 node
查看>>
NSArray数组
查看>>
Apache2.0x 开启gzip压缩
查看>>
关于驰骋表单设计器导入导出问题解决方案
查看>>
利用 XNA 实现 Windows Phone 7 上的电流效果
查看>>
phpcms学习
查看>>
Ubuntu13.10更新源
查看>>
我的友情链接
查看>>
java设计模式-工厂方法模式
查看>>
Java反射机制
查看>>
oVirt 杰云J60使用测试记录
查看>>
【OAuth2学习之路】Spring Security OAuth官网文档翻译
查看>>
Toad for DB2设置
查看>>
关于音视频的一些知识(demux、filter等)
查看>>
[笔记]改善Java程序的151个建议---第一章 Java开发中通用的方法和准则
查看>>
WindowsPhone7编辑器的设计器不显示Bug
查看>>
自己写JSON编辑器
查看>>
sql server中如何连接表更新数据
查看>>
高精度之——模板
查看>>
NetBeans和iReport的教程。
查看>>