本文主要总结一下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. Swift-9-类和结构体

    // Playground - noun: a place where people can play import UIKit // 几个重要的概念Properties/Methods/Subscr ...

  2. 如何给RecyclerView加上滚动条--现在就教你

    时隔许久,我又要更博了,含蓄的话不多说了,今天我们的主题是这个RecyclerView.至于为什么要加个scrollBar?因为我的业务需求是需要一个实现成这样的, 效果图:(可能看起来比较粗糙,但功 ...

  3. 第二百零七节,jQuery EasyUI,MenuButton(菜单按钮)组件

    jQuery EasyUI,MenuButton(菜单按钮)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 MenuButton(菜单按钮)组件的使用方法 ...

  4. 在java中,List是个接口,那实现List接口的类有哪些,有什么区别?

    在java中,List是个接口,那实现List接口的类有哪些,有什么区别? 解答: ArrayList是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引 ...

  5. OpenWrt 安装usb支持

    (一)下载软件 1)komd-usb-ohci kmod-usb2 kmod-usb-storage kmod-usb-core 这些是USB驱动包 2) kmod-nls-base kmod-nls ...

  6. WPF 附加属性的用法 (一)

    public class MDCTest { public static DependencyProperty MouseDoubleClickCommandProperty = Dependency ...

  7. 用ChemDraw画3D图的方法

    在绘制化学图形的时候,很多的用户都会发现很多的图形都是三维的,这个时候就需要找一款能够绘制3D图形的化学绘图软件.ChemOffice 15.1是最新的化学绘图工具套件,总共有三个组件,其中ChemD ...

  8. JDB调试之小试牛刀

    用JDK自带工具JDB调试示例程序HelloJDB(d:\jdb\HelloJDB) HelloJDB代码如下: public class HelloJDB { public static void ...

  9. redis Could not connect to Redis at 127.0.0.1:6379: Connection refused 问题解决

    1.启动redis 客户端 redis-cli 报错 redis Could not connect to Redis at 127.0.0.1:6379: Connection refused 是因 ...

  10. LeetCode-Pathcing Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...