re模塊是python中處理正在表達式的一個模塊
正則表達式知識儲備:http://www.cnblogs.com/huamingao/p/6031411.html
1. match(pattern, string, flags=0)
從字符串的開頭進行匹配, 匹配成功就返回一個匹配對象,匹配失敗就返回None
flags的幾種值
X 忽略空格和注釋
I 忽略大小寫的區別 case-insensitive matching
S . 匹配任意字符,包括新行

def match(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning a match object, or None if no match was found."""
return _compile(pattern, flags).match(string)
2. search(pattern, string, flags=0)
瀏覽整個字符串去匹配第一個,未匹配成功返回None

def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found."""
return _compile(pattern, flags).search(string)
3. findall(pattern, string, flags=0)
match和search均用於匹配單值,即:只能匹配字符串中的一個,如果想要匹配到字符串中所有符合條件的元素,則需要使用 findall。
findall,獲取非重復的匹配列表;如果有一個組則以列表形式返回,且每一個匹配均是字符串;如果模型中有多個組,則以列表形式返回,且每一個匹配均是元祖;
空的匹配也會包含在結果中

def findall(pattern, string, flags=0): """Return a list of all non-overlapping matches in the string. If one or more capturing 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."""
return _compile(pattern, flags).findall(string)
4. sub(pattern,repl,string,count=0,flags=0)
替換匹配成功的指定位置字符串

def sub(pattern, repl, string, count=0, flags=0): """Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)
根據正則匹配分割字符串

def split(pattern, string, maxsplit=0, flags=0): """Split the source string by the occurrences of the pattern, returning a list containing the resulting substrings. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list."""
return _compile(pattern, flags).split(string, maxsplit)
6.compile()
python代碼最終會被編譯為字節碼,之后才被解釋器執行。在模式匹配之前,正在表達式模式必須先被編譯成regex對象,預先編譯可以提高性能,re.compile()就是用於提供此功能。
7. group()與groups()
匹配對象的兩個主要方法:
group() 返回所有匹配對象,或返回某個特定子組,如果沒有子組,返回全部匹配對象
groups() 返回一個包含唯一或所有子組的的元組,如果沒有子組,返回空元組

def group(self, *args): """Return one or more subgroups of the match. :rtype: T | tuple """
pass
def groups(self, default=None): """Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. :rtype: tuple """
pass