跳到正文

re正则模块详解

大数据 1715 次浏览

re正则表达式是一种工具,和其他工具一样,它是人们为了解决某一类专门的问题而发明的。想要理解正则表达式及其功用。最好的办法是了解他们可以解决什么样的问题

正则表达式的主要用途之一是搜索变化多端的文本,比如,搜索以http开头的 以句号等空白字结尾的字符串 


import re

text = “The Attila the Hun Show”

a single character 单个字符

m = re.match(".", text) if m: print repr("."), “=>”, repr(m.group(0))

any string of characters 任何字符串

m = re.match(".", text) if m: print repr("."), “=>”, repr(m.group(0))

a string of letters (at least one) 只包含字母的字符串(至少一个)

m = re.match("\w+", text) if m: print repr("\w+"), “=>”, repr(m.group(0))

a string of digits 只包含数字的字符串

m = re.match("\d+", text) if m: print repr("\d+"), “=>”, repr(m.group(0))

B ‘.’ => ‘T’ ‘.*’ => ‘The Attila the Hun Show’ ‘\w+’ => ‘The’b ```

可以使用圆括号在模式中标记区域. 找到匹配后, group 方法可以抽取这些区域的内容,

如 [Example 1-55 #eg-1-55] 所示. group(1) 会返回第一组的内容, group(2)

返回第二组的内容, 这样… 如果你传递多个组数给 group 函数, 它会返回一个元组

使用 re 模块抽出匹配的子字符串

import re

text =“10/15/99”

m = re.match("(\d{2})/(\d{2})/(\d{2,4})", text) if m: print m.group(1, 2, 3)

B(‘10’, ‘15’, ‘99’)b

 
``search`` 函数会在字符串内查找模式匹配, 如 [Example 1-56 #eg-1-56] 所示.
它在所有可能的字符位置尝试匹配模式, 从最左边开始, 一旦找到匹配就返回一个匹配对象.
如果没有找到相应的匹配, 就返回 //None// .
 
====Example 1-56. 使用 re 模块搜索子字符串====[eg-1-56]
 

File: re-example-3.py

import re

text = “Example 3: There is 1 date 10/25/95 in here!”

m = re.search("(\d{1,2})/(\d{1,2})/(\d{2,4})", text)

print m.group(1), m.group(2), m.group(3)

month, day, year = m.group(1, 2, 3) print month, day, year

date = m.group(0) print date

B10 25 95 10 25 95 10/25/95b

 
text = "you're no fun anymore..."
 
# literal replace (string.replace is faster)
# 文字替换 (string.replace 速度更快)
print re.sub("fun", "entertaining", text)
 
# collapse all non-letter sequences to a single dash
# 将所有非字母序列转换为一个"-"(dansh,破折号)
print re.sub("[^\w]+", "-", text)
 
# convert all words to beeps
# 将所有单词替换为 BEEP
print re.sub("\S+", "-BEEP-", text)
 
*B*you're no entertaining anymore...
you-re-no-fun-anymore-
-BEEP- -BEEP- -BEEP- -BEEP-*b*

你也可以通过回调 (callback) 函数使用 sub 来替换指定模式. [Example 1-58 #eg-1-58] 展示了如何预编译模式.

====Example 1-58. 使用 re 模块替换字符串(通过回调函数)====[eg-1-58]

File: re-example-5.py
 
import re
import string
 
text = "a line of text\\012another line of text\\012etc..."
 
def octal(match):
    # replace octal code with corresponding ASCII character
    # 使用对应 ASCII 字符替换八进制代码
    return chr(string.atoi(match.group(1), 8))
 
octal_pattern = re.compile(r"\\(\d\d\d)")
 
print text
print octal_pattern.sub(octal, text)
 
*B*a line of text\012another line of text\012etc...
a line of text
another line of text
etc...*b*
```</code></pre><div class="line number151 index150 alt2"><code class="python plain">如果你不编译, ``re`` 模块会为你缓存一个编译后版本, 所有的小脚本中, </code></div><p></p><div class="line number152 index151 alt1"><code class="python plain">通常不需要编译正则表达式. Python1.</code><code class="python value">5.2</code> <code class="python plain">中, 缓存中可以容纳 </code><code class="python value">20</code> <code class="python plain">个匹配模式, 而在 </code><code class="python value">2.0</code> <code class="python plain">中, </code></div><p><code class="python plain"></code></p><p><code class="python plain">缓存则可以容纳 </code><code class="python value">100</code> <code class="python plain">个匹配模式</code></p><p><code class="python plain">使用 re 模块匹配多个模式中的一个</code></p><p><code class="python plain"></code></p><pre lay-lang="Python" class="layui-code" skin="notepad"><code class="python plain">import re, string
 
def combined_pattern(patterns):
    p = re.compile(
        string.join(map(lambda x: "("+x+")", patterns), "|")
        )
    def fixup(v, m=p.match, r=range(0,len(patterns))):
        try:
            regs = m(v).regs
        except AttributeError:
            return None # no match, so m.regs will fail
        else:
            for i in r:
                if regs[i+1] != (-1, -1):
                    return i
    return fixup
 
#
# try it out!
 
patterns = [
    r"\d+",
    r"abc\d{2,4}",
    r"p\w+"
]
 
p = combined_pattern(patterns)
 
print p("129391")
print p("abc800")
print p("abc1600")
print p("python")
print p("perl")
print p("tcl")
 
*B*0
1
1
2
2
None*b*
```</code></pre><p></p><p><br></p>

评论

加载中…