本文主要总结一下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. 禁用LinkButton的方法

    1.服务器端,使用Enabled属性即可 <asp:LinkButton ID="lbtn" runat="server" Enabled="f ...

  2. petrozavodsk summer 2018 游记&&总结

    day0: 出发前训了一场比较水bapc2017保持手感(恢复信心),成功AK了,不过罚时略高.然后三人打车从紫金港到杭州东站,坐高铁到上海虹桥,再坐机场快线到浦东机场(傻乎乎的jsb帮爸爸付了钱,然 ...

  3. 富文本编辑期Quill

    官方网站http://quilljs.com/ 使用方法 <!-- Create the toolbar container --> <div id="toolbar&qu ...

  4. django用户认证系统——基本设置1

    网站提供登录.注册等用户认证功能是一个常见的需求.因此,Django 提供了一套功能完整的.灵活的.易于拓展的用户认证系统:django.contrib.auth.在本教程中,我将向你展示 auth ...

  5. 59、常规控件(2)TextInputLayout-让EditText提示更加人性化

    提示语用在显示. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...

  6. 【BZOJ3190】[JLOI2013]赛车 单调栈+几何

    [BZOJ3190][JLOI2013]赛车 Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2……gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进 ...

  7. css生成三角形

     转载:http://www.cnblogs.com/lhb25/p/css-and-css3-triangle.html Triangle Up    1 2 3 4 5 6 7 #triangle ...

  8. 170122、Netty 长连接服务

    推送服务 还记得一年半前,做的一个项目需要用到 Android 推送服务.和 iOS 不同,Android 生态中没有统一的推送服务.Google 虽然有 Google Cloud Messaging ...

  9. 160621、Java注解教程及自定义注解

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  10. 从global到mooncake迁移SQL Azure

    之前遇到了问题,在此备注一下: 因为两个环境基本上可以认为是隔离的,所以迁移过程基本上只有通过导出.导入的方式(也是官方推荐的方式): 1.从global上进行数据库的export操作(扩展名bacp ...