2.7小节完成

This commit is contained in:
XiongNeng
2014-09-04 01:02:03 +08:00
parent 91dd8ec12f
commit edd336e027
2 changed files with 65 additions and 3 deletions

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 最短匹配,非贪婪模式匹配
Desc :
"""
import re
def short_match():
# 贪婪模式
str_pat = re.compile(r'\"(.*)\"')
text1 = 'Computer says "no."'
print(str_pat.findall(text1))
text2 = 'Computer says "no." Phone says "yes."'
print(str_pat.findall(text2))
# 非贪婪模式
str_pat = re.compile(r'\"(.*?)\"')
print(str_pat.findall(text2))
if __name__ == '__main__':
short_match()

View File

@@ -5,14 +5,51 @@
----------
问题
----------
todo...
你正在试着用正则表达式匹配某个文本模式,但是它找到的是模式的最长可能匹配。
而你想修改它变成查找最短的可能匹配。
|
----------
解决方案
----------
todo...
这个问题一般出现在需要匹配一对分隔符直接的文本的时候(比如引号包含的字符串)。
为了说明清楚,考虑如下的例子:
.. code-block:: python
>>> str_pat = re.compile(r'\"(.*)\"')
>>> text1 = 'Computer says "no."'
>>> str_pat.findall(text1)
['no.']
>>> text2 = 'Computer says "no." Phone says "yes."'
>>> str_pat.findall(text2)
['no." Phone says "yes.']
>>>
在这个例子中模式r'\"(.*)\"'的意图是匹配包含在双引号中的文本。
但是在正则表达式中*操作符是贪婪的,因此匹配操作会查找最长的可能匹配。
于是在第二个例子中搜索text2的时候返回结果并不是我们想要的。
为了修正这个问题,可以在模式中的*操作符后面加上?修饰符,就像这样:
.. code-block:: python
>>> str_pat = re.compile(r'\"(.*?)\"')
>>> str_pat.findall(text2)
['no.', 'yes.']
>>>
这样就使得匹配变成非贪婪模式,从而得到最短的匹配,也就是我们想要的结果。
|
----------
讨论
----------
todo...
这一节展示了在写包含点(.)字符的正则表达式的时候遇到的一些常见问题。
在一个模式字符串中,点(.)匹配除了换行外的任何字符。
然而,如果你将点(.)号放在开始与结束符(比如引号)之间的时候,那么匹配操作会查找符合模式的最长可能匹配。
这样通常会导致很多出现在开始与结束符之间的匹配被忽略掉,并最终被包含在匹配结果字符串中返回。
通过在*或者+这样的操作符后面添加一个?可以强制匹配算法改成寻找最短的可能匹配。