Python 正则表达式 Howto(5)

模块级的函数(非模式对象)

其实没有必要创建模式对象,调用对象的方法。Re模块同时提供了一个全局的函数,也叫match()search()findall()sub(), (函数重载?静态函数吧)这些函数跟模式对象的函数采用基本上一样的参数,除了我们要把模式串作为第一个参数传递给这些函数之外。同样这些函数也返回None或者是匹配对象。

>>> 

>>> print re.match(r'From\s+', 'Fromage amk')

None

>>> re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998'

<_sre.SRE_Match objectat 0x...>

其实在这些简单的语句后面,这些函数也是简答的创建一个模式对象,并且调用相关的函数。这些函数对象存放在cache中以备将来快速调用。

我们到底是应该调用全局的函数呢?还是创建一个模式对象然后调用其方法呢?这取决于匹配的模式的使用频率,还有就是取决于你的编码风格。如果这些对象模式只是偶尔用用,那么全局的函数最方便。如果我们要在程序中使用大量的正则表达式,或者是我们要多次使用一个,我们最好是把所有的模式集中起来,在使用之前编译好。我们来看一下 xmllib模块的例子:

ref = re.compile( ... )

entityref = re.compile( ... )

charref = re.compile( ... )

starttagopen = re.compile( ... )

我基本上习惯使用编译的模式对象,即使使用一次我也是使用编译好的对象。也有跟我完全相反的人,即使是使用很多次也是用全局的函数。不牵扯效率问题,完全取决于你自己的喜好。

编译选项

模式对象的编译选项可以更改正则表达式的工作方式。或者更改正则的含义。编译选项在RE模块中一般都有两个名字,一个长名字,如IGNORECASE 短名字叫I。 如果你是一个Perl 的Fans,那么你有福了, 这些短名字跟Perl 的一样。多个flag可以或起来, 如: re.I | re.M 可以设置M跟I。

下表是多有支持的flag 的表及其解释:

选项

含义

DOTALLS

改变.的含义,使其可以匹配包括换行在内的所有字符

IGNORECASEI

匹配是忽略大小写

LOCALEL

区域标识

MULTILINEM

多行匹配, 包括^以及$

VERBOSEX

Enable verbose REs, which can be organized more cleanly and understandably.

UNICODEU

Makes several escapes like \w\b\s and \d dependent on the Unicode character database.

I

IGNORECASE

进行大小写不敏感的匹配,字符类以及具体的字符都是大小写不敏感的。例如 [A-Z] 也可以匹配小写字母, Spam 会匹配 Spam, spam, or spAM. 如果你不设置LOCALE ,大小写就不会考虑区域这个方面。

L

LOCALE

让 \w, \W, \b, and \B,支持当前的语言(地域)设置.(下文中沿用Locale)

Locales是C语言中的一个功能,这个功能主要是让C语言程序的编写消除语言的差异。如果你使用的是法语,但是你想使用 \w+ 匹配单词,但是 \w 只是匹配字母位于 [A-Za-z]中的单词; 他不会匹配 'é' 以及 'ç'.如果你的系统正确的配置了Locales,某些C函数会认为 'é' 也应该认为是一个字符. 当编译正则表达式的时候设置 LOCALE 标识flag或使用这些C函数来使用 \w;这些函数比较慢但是可以让 \w+ 匹配法文单词。

M

MULTILINE

Usually ^ matches only at thebeginning of the string, and $ matches only at theend of the string and immediately before the newline (if any) at the end of thestring. When this flag is specified, ^ matches at thebeginning of the string and at the beginning of each line within the string,immediately following each newline. Similarly, the $ metacharacter matcheseither at the end of the string and at the end of each line (immediatelypreceding each newline).

S

DOTALL

Makes the '.' special charactermatch any character at all, including a newline; without this flag, '.' will match anything except anewline.

U

UNICODE

Make \w, \W, \b, \B, \d, \D, \s and \S dependent on theUnicode character properties database.

X

VERBOSE

This flag allows you towrite regular expressions that are more readable by granting you moreflexibility in how you can format them. When this flag has been specified,whitespace within the RE string is ignored, except when the whitespace is in acharacter class or preceded by an unescaped backslash; this lets you organizeand indent the RE more clearly. This flag also lets you put comments within aRE that will be ignored by the engine; comments are marked by a '#'that’s neither in acharacter class or preceded by an unescaped backslash.

For example, here’s a REthat uses re.VERBOSE; see how much easier it is to read?

charref = re.compile(r"""

 &[#]                # Start of a numeric entityreference

 (

     0[0-7]+         # Octal form

   | [0-9]+          # Decimal form

   | x[0-9a-fA-F]+   # Hexadecimal form

 )

 ;                   # Trailing semicolon

""", re.VERBOSE)

Without the verbosesetting, the RE would look like this:

charref = re.compile("&#(0[0-7]+"

                     "|[0-9]+"

                     "|x[0-9a-fA-F]+);")

In the above example,Python’s automatic concatenation of string literals has been used to break upthe RE into smaller pieces, but it’s still more difficult to understand thanthe version using re.VERBOSE.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值