python與Regex:re模組詳解

來源:互聯網
上載者:User

標籤:.com   line   www.   gif   ring   self   regex   模式   分割   

re模組是python中處理正在運算式的一個模組

Regex知識儲備: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)
 5. split(pattern,string,maxsplit=0,flags=0) 根據正則匹配分割字串
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
 原文地址:http://www.cnblogs.com/huamingao/p/6062367.html

python與Regex:re模組詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.