$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)
随机推荐
- 对Linux命令进一步学习
root@wuheng-virtual-machine:/home/wuheng# ls -ltotal 44drwxr-xr-x 2 wuheng wuheng 4096 Mar 3 01:30 ...
- nodejs之路探寻
在webpack配置中经常会发现 const path = require('path') 这是加载nodejs路径处理API,这个API主要包含下面三个方法 path.dirname(filepat ...
- Delphi TreeView – 自动展开树形结构
Delphi TreeView – 自动展开树形结构 当处理完TreeView控件树形结构的数据后,需要默认自动全部展开,可以用到TreeView的Expanded属性. 1 2 3 4 5 6 7 ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- matplotlib绘制圆饼图
import matplotlib.pyplot as plt labels = ['Nokia','Samsung','Apple','Lumia'] values = [10,30,45,15] ...
- Byzantine failures
https://baike.baidu.com/item/拜占庭将军问题/265656?fr=aladdin 拜占庭将军问题(Byzantine failures),是由莱斯利·兰伯特提出的点对点通信 ...
- Java Concurrency In Practice
线程安全 定义 A class is thread-safe if it behaves correctly when accessed from multiple threads, regardle ...
- Storm-源码分析- spout (backtype.storm.spout)
1. ISpout接口 ISpout作为实现spout的核心interface, spout负责feeding message, 并且track这些message. 如果需要Spout track发出 ...
- django ORM 批量操作:批量插入bulk_create
django批量create数据:bulk_create(list实例) 项目中看到这样一句 models.表名.objects.using('数据库名').bulk_create(list实例) 其 ...
- 访问HDFS报错:org.apache.hadoop.security.AccessControlException: Permission denied
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apac ...