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

Python re module

編輯:Python

Module introduction

re modular , It provides Perl Style regular expression pattern .re Module enable Python The language has all the regular expression functions .re The module also provides functions that are fully consistent with the functions of these methods , These functions use a pattern string as their first argument .

Module function

1、match function

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

Parameters pattern A regular expression that represents a match , Parameters string Represents the string to match , Parameters flags Represents how regular expressions are matched , Multiple flags can be passed by biting OR(|) They specify . The optional values are as follows :

Modifier describe re.I Make match match case insensitive re.L Do localization identification (locale-aware) matching re.M Multi-line matching , influence ^ and $re.S send . Match all characters including line breaks re.U according to Unicode Character set parsing characters . This sign affects \w, \W, \b, \B.re.X This flag allows you to write regular expressions more easily by giving you a more flexible format .

re.match Try to match a pattern... From the beginning of the string , If it's not a successful start match ,match() Just go back to none.

>>> pattern = r"Cats"
>>> string = "Cats are smarter than dogs"
>>> re.match(pattern, string)
<re.Match object; span=(0, 4), match='Cats'>
# Set up flags=re.I Make expressions case insensitive
>>> pattern = r"cats"
>>> re.match(pattern, string, flags=re.I)
<re.Match object; span=(0, 4), match='Cats'>

2、search function

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

Scan the entire string and return the first successful match .

>>> 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 Match only the beginning of the string , If the string doesn't start with a regular expression , The match fails , The function returns None, and re.search Match the entire string , Until we find a match .

3、sub function

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

Replace the matching item in the string with the specified string . Parameters pattern A regular expression that represents a match , Parameters repl Represents the replacement string , Parameters string Represents the string to match , Parameters count Represents the maximum number of substitutions after pattern matching , Default 0 Means to replace all matches , Parameters flags Represents how regular expressions match .

# use is Replace in string are
>>> pattern = r"are"
>>> string = "Cats are smarter than dogs"
>>> repl = "is"
>>> re.sub(pattern, repl, string)
'Cats is smarter than dogs'

4、compile function

re.compile(pattern, flags=0)

Compile regular expressions , Generate a regular expression ( Pattern ) object , for match() and search() 、findall()、sub() And other functions .

>>> 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 function

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

Find all the substrings that the regular expression matches in the string , And return a list , If there are multiple matching patterns , The tuple list is returned , If no match is found , Then return to the empty list .

>>> 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']

Be careful : match and search It's a match , and findall Match all .

6、finditer function

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

Find all the substrings that the regular expression matches in the string , And return them as an iterator .

>>> 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 function

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

Split the string according to the matching substring and return to the list . Parameters maxsplit Indicates the number of divisions ,maxsplit=1 Split once , The default is 0, Unlimited times .

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

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