正则表达式

cheatsheet

Python Regular Expression Quick Guide

  • ^ Matches the beginning of a line $ Matches the end of the line
  • . Matches any character
  • \s Matches whitespace
  • \S Matches any non­whitespace character
  • * Repeats a character zero or more times
  • *? Repeats a character zero or more times (non­greedy)
  • + Repeats a character one or more times
  • +? Repeats a character one or more times(non­greedy)
  • [aeiou] Matches a single character in the listed set
  • [^XYZ] Matches a single character not in the listed set
  • [a­-z0-­9] The set of characters can in clude arange
  • ( Indicates where string extraction is to start
  • ) Indicates where string extraction is to end

re库里的文档

The special characters are:
    "."      Matches any character except a newline.
    "^"      Matches the start of the string.
    "$"      Matches the end of the string or just before the newline at
             the end of the string.
    "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
             Greedy means that it will match as many repetitions as possible.
    "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
    "?"      Matches 0 or 1 (greedy) of the preceding RE.
    *?,+?,?? Non-greedy versions of the previous three special characters.
    {m,n}    Matches from m to n repetitions of the preceding RE.
    {m,n}?   Non-greedy version of the above.
    `"\\"``    Either escapes special characters or signals a special sequence.
    []       Indicates a set of characters.
             A "^" as the first character indicates a complementing set.
    "|"      A|B, creates an RE that will match either A or B.
    (...)    Matches the RE inside the parentheses.
             The contents can be retrieved or matched later in the string.
    (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below).
    (?:...)  Non-grouping version of regular parentheses.
    (?P<name>...) The substring matched by the group is accessible by name.
    (?P=name)     Matches the text matched earlier by the group named name.
    (?#...)  A comment; ignored.
    (?=...)  Matches if ... matches next, but doesn't consume the string.
    (?!...)  Matches if ... doesn't match next.
    (?<=...) Matches if preceded by ... (must be fixed length).
    (?<!...) Matches if not preceded by ... (must be fixed length).
    (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                       the (optional) no pattern otherwise.
The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.
    \number  Matches the contents of the group of the same number.
    \A       Matches only at the start of the string.
    \Z       Matches only at the end of the string.
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       Matches any decimal digit; equivalent to the set [0-9].
    \D       Matches any non-digit character; equivalent to the set [^0-9].
    \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v].
    \S       Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v].
    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.
This module exports the following functions:
    match    Match a regular expression pattern to the beginning of a string.
    search   Search a string for the presence of a pattern.
    sub      Substitute occurrences of a pattern found in a string.
    subn     Same as sub, but also return the number of substitutions made.
    split    Split a string by the occurrences of a pattern.
    findall  Find all occurrences of a pattern in a string.
    finditer Return an iterator yielding a match object for each match.
    compile  Compile a pattern into a RegexObject.
    purge    Clear the regular expression cache.
    escape   Backslash all non-alphanumerics in a string.

练习1

从sample.txt里提取所有数字,然后求和

import re
print = sum([int(x) for x in re.findall('[0-9]+',open('sample.txt').read())])

greedy

正则表达式里默认使用贪婪的搜索方法,也就是搜索能匹配的最长字符串,使用?可以取消贪婪

import re
x = 'From: Using the : character'
y = re.findall('^F.+:', x)
print y
['From: Using the :']
import re
x = 'From: Using the : character'
y = re.findall('^F.+?:', x)  # 加了个`?`
print y
['From:']

练习2

    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.

\w对应字母和数字,\W\w的补集

所以想去处字母和数字之外的字符,就可以用

import re
re.sub('\W+', '', my_string)

如下例

n [1]: x = '#*cat36*:'

In [2]: import re

In [3]: re.sub('\W+', '', x)
Out[3]: 'cat36'

另外还有一种方法是用str.translate()函数

这个函数一般是用于字符转换,但不提供第一个参数,而把要删除的字符写进第二个参数时,可以用来做字符删除。如

In [4]: "***foo ?! bar".translate(None, "*?!")
Out[4]: 'foo  bar'

练习3

处理HTTP header信息

已知有这样的header信息

header = "Host: www.google.com\nConnection: keep-alive\nAccept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\nUser-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.45 Safari/534.13\nAccept-Encoding: gzip,deflate,sdch\nAvail-Dictionary: GeNLY2f-\nAccept-Language: en-US,en;q=0.8\n"

想要将其转换成一个字典,可以使用正则的(?P...)

import re
headers = dict(re.findall(r"(?P<name>.*?): (?P<value>.*?)\n", header))
print headers

results matching ""

    No results matching ""