程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python re模塊

編輯:Python

模塊介紹

re 模塊,它提供 Perl 風格的正則表達式模式。re 模塊使 Python 語言擁有全部的正則表達式功能。re 模塊也提供了與這些方法功能完全一致的函數,這些函數使用一個模式字符串做為它們的第一個參數。

模塊函數

1、match函數

re.match(pattern, string, flags=0)

參數pattern表示匹配的正則表達式,參數string表示要匹配的字符串,參數flags表示用於控制正則表達式的匹配方式,多個標志可以通過按位 OR(|) 它們來指定。可選的值如下:

修飾符描述re.I使匹配對大小寫不敏感re.L做本地化識別(locale-aware)匹配re.M多行匹配,影響 ^ 和 $re.S使 . 匹配包括換行在內的所有字符re.U根據Unicode字符集解析字符。這個標志影響 \w, \W, \b, \B.re.X該標志通過給予你更靈活的格式以便你將正則表達式寫得更易於理解。

re.match 嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。

>>> pattern = r"Cats"
>>> string = "Cats are smarter than dogs"
>>> re.match(pattern, string)
<re.Match object; span=(0, 4), match='Cats'>
# 設置flags=re.I使表達式對大小寫不敏感
>>> pattern = r"cats"
>>> re.match(pattern, string, flags=re.I)
<re.Match object; span=(0, 4), match='Cats'>

2、search函數

re.search(pattern, string, flags=0)

掃描整個字符串並返回第一個成功的匹配。

>>> pattern = r"are"
>>> string = "Cats are smarter than dogs"
>>> re.match(pattern, string)
None
>>> re.search(pattern, string)
<re.Match object; span=(5, 8), match='are'>

re.match 只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回 None,而 re.search 匹配整個字符串,直到找到一個匹配。

3、sub函數

re.sub(pattern, repl, string, count=0, flags=0)

用指定字符串替換字符串中的匹配項。參數pattern表示匹配的正則表達式,參數repl表示替換的字符串,參數string表示要匹配的字符串,參數count表示模式匹配後替換的最大次數,默認 0 表示替換所有的匹配,參數flags表示正則表達式的匹配方式。

# 用is替換字符串中的are
>>> pattern = r"are"
>>> string = "Cats are smarter than dogs"
>>> repl = "is"
>>> re.sub(pattern, repl, string)
'Cats is smarter than dogs'

4、compile函數

re.compile(pattern, flags=0)

編譯正則表達式,生成一個正則表達式( Pattern )對象,供 match() 和 search() 、findall()、sub()等多個函數使用。

>>> pattern = re.compile(r"cats", flags=re.I)
>>> pattern
re.compile('cats', re.IGNORECASE)
>>> pattern.match("Cats are smarter than dogs")
<re.Match object; span=(0, 4), match='Cats'>
>>> pattern.search("Cats are smarter than dogs")
<re.Match object; span=(0, 4), match='Cats'>

5、findall函數

re.findall(pattern, string, flags=0)

在字符串中找到正則表達式所匹配的所有子串,並返回一個列表,如果有多個匹配模式,則返回元組列表,如果沒有找到匹配的,則返回空列表。

>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.match(pattern, string)
<re.Match object; span=(0, 3), match='123'>
>>> re.search(pattern, string)
<re.Match object; span=(0, 3), match='123'>
>>> re.findall(pattern, string)
['123', '12', '3']

注意: match 和 search 是匹配一次 ,而findall 匹配所有。

6、finditer函數

re.finditer(pattern, string, flags=0)

在字符串中找到正則表達式所匹配的所有子串,並把它們作為一個迭代器返回。

>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.findall(pattern, string)
['123', '12', '3']
>>> re.finditer(pattern, string)
<callable_iterator object at 0x0000029B2DEA3EE0>

7、split函數

re.split(pattern, string, maxsplit=0, flags=0)

按照能夠匹配的子串將字符串分割後返回列表。參數maxsplit表示分割次數,maxsplit=1 分割一次,默認為 0,不限制次數。

>>> pattern = r"\d+"
>>> string = "123 is not 12 and 3"
>>> re.split(pattern, string)
['', ' is not ', ' and ', '']

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved