$python正则表达式系列(6)——"或"表达式的用法
import re
s1 = u'距离地铁5号线189米'
s2 = u'距离地铁5号线(环中线)189米'
s3 = u'距离地铁5号线(环中线)189米'
p1 = re.compile(u'号线(\d+)米')
print re.findall(p1,s1)
# 输出:[u'189']
print re.findall(p1,s2)
# 输出:[]
print re.findall(p1,s3)
# 输出:[]
p2 = re.compile(u'(?:号线|\)|))(\d+)') # 注:这里的'?:'是为了取消分组,不在结果中捕获
print re.findall(p2,s1)
# 输出:[u'189']
print re.findall(p2,s2)
# 输出:[u'189']
print re.findall(p2,s3)
# 输出:[u'189']
# 匹配java中的单行注释的正则表达式:
p1 = r'^\s*(//.*$|/\*.*\*/\s*$|$)'
# 匹配java中的多行注释的正则表达式:
code_text = '...'
p2 = r'/\*.+?\*/'
result = re.findall(p2,code_text,re.S)
随机推荐
- VS2010之– Web Development(四)-将WebApplication打包发布到IIS
下面将介绍怎样将一个WebApplication在VisualStudio中直接打包发布到IIS上去. 首先需要使用Administrator的身份运行Visual Studio. Task 1 – ...
- Openstack(Kilo)安装系列之neutron(九)
控制节点 Before you configure the OpenStack Networking (neutron) service, you must create a database, se ...
- Android实例-退出程序
Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...
- Hibernate 中多对多(many-to-many)关系的查询语句
两个对象: 学生表:Student 课程表:Course 两者的关系是多对多,当查询Student对象,并以Course对象作为条件时的sql语句写法如下: select pa from Studen ...
- iOS 百度地图获取当前地理位置
// // ViewController.m // BaiDuDemo // // Created by Chocolate. on 15-3-2. // Copyright (c) 2015年 re ...
- mysql数据库导入到oracle数据库
首先,写一个cmd脚本 xx.cmd sqlldr username/password control=xx.ctl errors=10000000 direct=y 再写一个bat脚本xx.bat ...
- 诡异的js
[] + {}; 隐式转换后,是0 那 {} + []呢? var a = 42,b; b = ( a++, a);
- iOS开发——生命周期
为了处理好应用程序的挂起.暂停等情况下的数据保存,或对应添加所需处理,我们必须了解ios生命周期. 但是不要去背去记,做个实验就好. - (BOOL)application:(UIApplicatio ...
- 网站行为跟踪 Website Activity Tracking Log Aggregation 日志聚合 In comparison to log-centric systems like Scribe or Flume
网站行为跟踪 Website Activity Tracking 访客信息处理 Log Aggregation 日志聚合 Apache Kafka http://kafka.apache.org/ ...
- 常用的SQLalchemy 字段类型
https://blog.csdn.net/weixin_41896508/article/details/80772238 常用的SQLAlchemy字段类型 类型名 python中类型 说明 In ...