本文主要总结一下python正则的一些内置属性的用法。

1. 编译标志:flags

首先来看一下re.findall函数的函数原型:

import re
print('【Output】')
print help(re.findall)
【Output】
Help on function findall in module re: findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string. If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group. Empty matches are included in the result. None

可以看出,re.findall函数的最后一个参数是flags,默认值是0,这个falgs就是编译标志,即正则的内置属性,使用不同的编译标志可以让正则产生不同的匹配效果。那么falgs可以取哪些值呢?用help(re)来看一下re的DATA有哪些:

print help(re)

# 【Output】
'''
...
DATA
DOTALL = 16
I = 2
IGNORECASE = 2
L = 4
LOCALE = 4
M = 8
MULTILINE = 8
S = 16
U = 32
UNICODE = 32
VERBOSE = 64
X = 64
...
'''

下面试验一下上面的每一种编译标志的作用。

2. DOTALL, S

使"."匹配包括"\n"在内的所有字符("."默认是不能匹配"\n“的),举例:

p = r'me.com'
print '【Output】'
print re.findall(p,'me.com')
print re.findall(p,'me\ncom')
print re.findall(p,'me\ncom',re.DOTALL)
print re.findall(p,'me\ncom',re.S)
【Output】
['me.com']
[]
['me\ncom']
['me\ncom']

注:使用re.S模式时,正则表达式不能是编译后的正则(re.compile()函数),否则会出错。

使用re.S模式时,"^"字符变为文档开始符而不再是行开始符,"$"字符变为文档结束符而不再是行结束符。

3. IGNORECASE, I

使匹配对大小写不敏感,举例:

p = r'a'
print '【Output】'
print re.findall(p,'A')
print re.findall(p,'A',re.IGNORECASE)
print re.findall(p,'A',re.I)
【Output】
[]
['A']
['A']

4. LOCALE, L

本地化匹配,使用了该编译标志后,\w,\W,\b,\B,\s,\S等字符的含义就和本地化有关了。

5. MULTILINE, M

开启多行匹配,影响"^"和"$"。举例:

s = """
aa bb cc
bb aa
aa ccd
"""
p1 = r'^aa'
p2 = r'cc$'
print '【Output】'
print re.findall(p1,s)
print re.findall(p1,s,re.M) print re.findall(p2,s)
print re.findall(p2,s,re.M)
【Output】
[]
['aa', 'aa']
[]
['cc']

6. VERBOSE, X

开启正则的多行写法,使之更清晰。举例:

p = r"""
\d{3,4}
-?
\d{7,8}
"""
tel = '010-12345678'
print '【Output】'
print re.findall(p,tel)
print re.findall(p,tel,re.X)
【Output】
[]
['010-12345678']

7. UNICODE, U

以unicode编码进行匹配,比如用'\s'匹配中文全角的空格符:\u3000,不加该编译标志和加该编译标志的效果对比如下:

s = u'\u3000'
p = r'\s'
print '【Output】'
print re.findall(p,s)
print re.findall(p,s,re.U)
【Output】
[]
[u'\u3000']

8. 如何同时使用多个编译标志?

有时候可能同时要用到多种编译标志,比如我既想在匹配的时候忽略大小写,又想让"."匹配换行符号"\n",前面的方式貌似不行了,那怎么办呢?

方法:在正则的任意位置加上这句即可:(?iLmsux)

其中i对应re.I,L对应re.L,m对应re.M,s对应re.S,u对应re.U,x对应re.X。举例:

s = 'Abc\ncom'
p = r'abc.com(?is)' # 注:编译标志(?is)可以加在正则的任意位置,这里加在了末尾
print '【Output】'
print re.findall(p,s)
【Output】
['Abc\ncom']

随机推荐

  1. 【BZOJ】3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3397 显然先tarjan缩点,然后从枚举每一个scc,然后向其它岛屿连费用最小的边,然后算最小的即可 ...

  2. Android开源-NineOldAndroids

    开源地址: https://github.com/JakeWharton/NineOldAndroids 简单介绍:NineOldAndroids是一款支持在低版本号开发的Android动画的框架 包 ...

  3. (转)servlet setCharacterEncoding setContentType

    转自:http://blog.csdn.net/fancylovejava/article/details/7700683 编码中的setCharacterEncoding 理解 1.pageEnco ...

  4. 【ASK】git使用中出现Permission denied (publickey).

    好久没有用git了,今天突然执行了一下 $git submodule update --init --recursive =============================== 结果出现如下提 ...

  5. mybatis 处理 mysql 表中的 text类型的 字段

    在mysql 中 text类型的字段: service_detail text NULL 服务描述   . 对应java文件中 model 中的 String:  private String ser ...

  6. 在ubuntu机器上部署php测试环境

    在ubuntu机器上部署php测试环境 一.部署环境 Ubuntu11.10_X86_32,编译安装相应的软件:nginx+mysql+php. 二.软件安装 2.1 软件下载 libiconv-1. ...

  7. DotNet软件开发框架

    这是我4月份发在donews博客上的文章,现在都转到博客园来,风满袖希望进一步阐述你的架构,我就将这篇文章转移到博客园.原文:http://blog.donews.com/shanyou/archiv ...

  8. AngularJS 讲解,二 模块

    AngularJS允许我们使用angular.module()方法来声明模块,这个方法能够接受两个参数,第一个是模块的名称,第二个是依赖列表,也就是可以被注入到模块中的对象列表. angular.mo ...

  9. 160812、apache milagro分布式安全认证系统

    java32位.64位及js的代码:http://pan.baidu.com/s/1cqnwuE 一.云链接为中心的软件及需要互联网规模物联网设备 二.利用双线性密码学分发加密操作和分裂的加密参数 三 ...

  10. Python设置默认编码为UTF-8

    1.在Python\Lib\site-packages目录下创建一个sitecustomize.py文件 源代码: import sys sys.setdefaultencoding('utf-8') ...