RegEx正則表達(dá)

介紹演示

正則表達(dá)式 (Regular Expression) 又稱 RegEx, 是用來匹配字符的一種工具. 在一大串字符中尋找你需要的內(nèi)容. 它常被用在很多方面, 比如網(wǎng)頁(yè)爬蟲, 文稿整理, 數(shù)據(jù)篩選等等. 最簡(jiǎn)單的一個(gè)例子, 比如我需要爬取網(wǎng)頁(yè)中每一頁(yè)的標(biāo)題. 而網(wǎng)頁(yè)中的標(biāo)題常常是這種形式.

<title>我是標(biāo)題</ title>

而且每個(gè)網(wǎng)頁(yè)的標(biāo)題各不相同, 我就能使用正則表達(dá)式, 用一種簡(jiǎn)單的匹配方法, 一次性選取出成千上萬網(wǎng)頁(yè)的標(biāo)題信息. 正則表達(dá)式絕對(duì)不是一天就能學(xué)會(huì)和記住的, 因?yàn)楸磉_(dá)式里面的內(nèi)容非常多, 強(qiáng)烈建議, 現(xiàn)在這個(gè)階段, 你只需要了解正則里都有些什么, 不用記住, 等到你真正需要用到它的時(shí)候, 再反過頭來, 好好琢磨琢磨, 那個(gè)時(shí)候才是你需要訓(xùn)練自己記住這些表達(dá)式的時(shí)候.

簡(jiǎn)單的匹配

正則表達(dá)式無非就是在做這么一回事. 在文字中找到特定的內(nèi)容, 比如下面的內(nèi)容. 我們?cè)?“dog runs to cat” 這句話中尋找是否存在 “cat” 或者 “bird”.

# matching string
pattern1 = "cat"
pattern2 = "bird"
string = "dog runs to cat"
print(pattern1 in string)    # True
print(pattern2 in string)    # False

但是正則表達(dá)式絕非不止這樣簡(jiǎn)單的匹配, 它還能做更加高級(jí)的內(nèi)容. 要使用正則表達(dá)式, 首先需要調(diào)用一個(gè) python 的內(nèi)置模塊 re. 然后我們重復(fù)上面的步驟, 不過這次使用正則. 可以看出, 如果 re.search() 找到了結(jié)果, 它會(huì)返回一個(gè) match 的 object. 如果沒有匹配到, 它會(huì)返回 None. 這個(gè) re.search() 只是 re 中的一個(gè)功能, 之后會(huì)介紹其它的功能.

import re

# regular expression
pattern1 = "cat"
pattern2 = "bird"
string = "dog runs to cat"
print(re.search(pattern1, string))  # <_sre.SRE_Match object; span=(12, 15), match='cat'>
print(re.search(pattern2, string))  # None

<ins class="adsbygoogle" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4601203457616636" data-ad-slot="3397817325" data-adsbygoogle-status="done" data-overlap-observer-io="false" style="display: block; text-align: center; height: 159px; width: 617px;"><ins id="aswift_2_expand" style="display: inline-table; border: none; height: 159px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 617px; background-color: transparent;"><ins id="aswift_2_anchor" style="display: block; border: none; height: 159px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 617px; background-color: transparent; overflow: hidden;"><iframe id="aswift_2" name="aswift_2" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="617" height="159" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4601203457616636&output=html&h=159&slotname=3397817325&adk=3401585631&adf=960100261&w=636&fwrn=4&lmt=1583377291&rafmt=11&psa=0&guci=2.2.0.0.2.2.0.0&format=636x159&url=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F13-10-regular-expression%2F&flash=0&wgl=1&dt=1587802197986&bpp=3&bdt=388&idt=102&shv=r20200422&cbv=r20190131&ptt=9&saldr=aa&abxe=1&prev_fmts=700x90%2C250x250&correlator=3466323590182&frm=20&pv=1&ga_vid=1212726724.1583946939&ga_sid=1587802198&ga_hid=1016610859&ga_fc=0&iag=0&icsg=9130715816&dssz=30&mdo=0&mso=0&rplot=4&u_tz=480&u_his=9&u_java=0&u_h=1080&u_w=1920&u_ah=1040&u_aw=1920&u_cd=24&u_nplug=3&u_nmime=4&adx=484&ady=2282&biw=1903&bih=937&scr_x=0&scr_y=0&eid=21065214&oid=3&pvsid=322963587975057&pem=821&ref=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F&rx=0&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1040%2C1920%2C937&vis=1&rsz=%7C%7CeEbr%7C&abl=CS&pfx=0&fu=8344&bc=31&ifi=3&uci=a!3&btvi=2&fsb=1&xpc=I9411BAsV6&p=https%3A//morvanzhou.github.io&dtd=106" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!3" data-google-query-id="CMnao5CQg-kCFQQaKgodoykIYA" data-load-complete="true" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 617px; height: 159px;"></iframe></ins></ins></ins>

靈活匹配

除了上面的簡(jiǎn)單匹配, 下面的內(nèi)容才是正則的核心內(nèi)容, 使用特殊的 pattern 來靈活匹配需要找的文字.

如果需要找到潛在的多個(gè)可能性文字, 我們可以使用 [] 將可能的字符囊括進(jìn)來. 比如 [ab] 就說明我想要找的字符可以是 a 也可以是 b. 這里我們還需要注意的是, 建立一個(gè)正則的規(guī)則, 我們?cè)?pattern 的 “” 前面需要加上一個(gè) r 用來表示這是正則表達(dá)式, 而不是普通字符串. 通過下面這種形式, 如果字符串中出現(xiàn) “run” 或者是 “ran”, 它都能找到.

# multiple patterns ("run" or "ran")
ptn = r"r[au]n"       # start with "r" means raw string
print(re.search(ptn, "dog runs to cat"))    # <_sre.SRE_Match object; span=(4, 7), match='run'>

同樣, 中括號(hào) [] 中還可以是以下這些或者是這些的組合. 比如 [A-Z] 表示的就是所有大寫的英文字母. [0-9a-z] 表示可以是數(shù)字也可以是任何小寫字母.

print(re.search(r"r[A-Z]n", "dog runs to cat"))     # None
print(re.search(r"r[a-z]n", "dog runs to cat"))     # <_sre.SRE_Match object; span=(4, 7), match='run'>
print(re.search(r"r[0-9]n", "dog r2ns to cat"))     # <_sre.SRE_Match object; span=(4, 7), match='r2n'>
print(re.search(r"r[0-9a-z]n", "dog runs to cat"))  # <_sre.SRE_Match object; span=(4, 7), match='run'>

<ins class="adsbygoogle" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4601203457616636" data-ad-slot="3397817325" data-adsbygoogle-status="done" data-overlap-observer-io="false" style="display: block; text-align: center; height: 123px; width: 636px;"><ins id="aswift_3_expand" style="display: inline-table; border: none; height: 123px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent;"><ins id="aswift_3_anchor" style="display: block; border: none; height: 123px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent; overflow: hidden;"><iframe id="aswift_3" name="aswift_3" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="636" height="123" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4601203457616636&output=html&h=159&slotname=3397817325&adk=3401585631&adf=3131098319&w=636&fwrn=4&lmt=1583377291&rafmt=11&psa=0&guci=2.2.0.0.2.2.0.0&format=636x159&url=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F13-10-regular-expression%2F&flash=0&wgl=1&dt=1587802197989&bpp=2&bdt=391&idt=111&shv=r20200422&cbv=r20190131&ptt=9&saldr=aa&abxe=1&prev_fmts=700x90%2C250x250%2C636x159&correlator=3466323590182&frm=20&pv=1&ga_vid=1212726724.1583946939&ga_sid=1587802198&ga_hid=1016610859&ga_fc=0&iag=0&icsg=9130715816&dssz=30&mdo=0&mso=0&rplot=4&u_tz=480&u_his=9&u_java=0&u_h=1080&u_w=1920&u_ah=1040&u_aw=1920&u_cd=24&u_nplug=3&u_nmime=4&adx=484&ady=3137&biw=1903&bih=937&scr_x=0&scr_y=0&eid=21065214&oid=3&pvsid=322963587975057&pem=821&ref=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F&rx=0&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1040%2C1920%2C937&vis=1&rsz=%7C%7CeEbr%7C&abl=CS&pfx=0&fu=8344&bc=31&ifi=4&uci=a!4&btvi=3&fsb=1&xpc=RnhynsXM2u&p=https%3A//morvanzhou.github.io&dtd=115" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!4" data-google-query-id="CLy5o5CQg-kCFdgYKgodU_ACLQ" data-load-complete="true" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 636px; height: 123px;"></iframe></ins></ins></ins>

按類型匹配

除了自己定義規(guī)則, 還有很多匹配的規(guī)則時(shí)提前就給你定義好了的. 下面有一些特殊的匹配類型給大家先總結(jié)一下, 然后再上一些例子.

  • \d : 任何數(shù)字
  • \D : 不是數(shù)字
  • \s : 任何 white space, 如 [\t\n\r\f\v]
  • \S : 不是 white space
  • \w : 任何大小寫字母, 數(shù)字和 “” [a-zA-Z0-9]
  • \W : 不是 \w
  • \b : 空白字符 (在某個(gè)字的開頭或結(jié)尾)
  • \B : 空白字符 (在某個(gè)字的開頭或結(jié)尾)
  • \ : 匹配 \
  • . : 匹配任何字符 (除了 \n)
  • ^ : 匹配開頭
  • $ : 匹配結(jié)尾
  • ? : 前面的字符可有可無

下面就是具體的舉例說明啦.

# \d : decimal digit
print(re.search(r"r\dn", "run r4n"))           # <_sre.SRE_Match object; span=(4, 7), match='r4n'>
# \D : any non-decimal digit
print(re.search(r"r\Dn", "run r4n"))           # <_sre.SRE_Match object; span=(0, 3), match='run'>
# \s : any white space [\t\n\r\f\v]
print(re.search(r"r\sn", "r\nn r4n"))          # <_sre.SRE_Match object; span=(0, 3), match='r\nn'>
# \S : opposite to \s, any non-white space
print(re.search(r"r\Sn", "r\nn r4n"))          # <_sre.SRE_Match object; span=(4, 7), match='r4n'>
# \w : [a-zA-Z0-9_]
print(re.search(r"r\wn", "r\nn r4n"))          # <_sre.SRE_Match object; span=(4, 7), match='r4n'>
# \W : opposite to \w
print(re.search(r"r\Wn", "r\nn r4n"))          # <_sre.SRE_Match object; span=(0, 3), match='r\nn'>
# \b : empty string (only at the start or end of the word)
print(re.search(r"\bruns\b", "dog runs to cat"))    # <_sre.SRE_Match object; span=(4, 8), match='runs'>
# \B : empty string (but not at the start or end of a word)
print(re.search(r"\B runs \B", "dog   runs  to cat"))  # <_sre.SRE_Match object; span=(8, 14), match=' runs '>
# \\ : match \
print(re.search(r"runs\\", "runs\ to me"))     # <_sre.SRE_Match object; span=(0, 5), match='runs\\'>
# . : match anything (except \n)
print(re.search(r"r.n", "r[ns to me"))         # <_sre.SRE_Match object; span=(0, 3), match='r[n'>
# ^ : match line beginning
print(re.search(r"^dog", "dog runs to cat"))   # <_sre.SRE_Match object; span=(0, 3), match='dog'>
# $ : match line ending
print(re.search(r"cat$", "dog runs to cat"))   # <_sre.SRE_Match object; span=(12, 15), match='cat'>
# ? : may or may not occur
print(re.search(r"Mon(day)?", "Monday"))       # <_sre.SRE_Match object; span=(0, 6), match='Monday'>
print(re.search(r"Mon(day)?", "Mon"))          # <_sre.SRE_Match object; span=(0, 3), match='Mon'>

如果一個(gè)字符串有很多行, 我們想使用 ^ 形式來匹配行開頭的字符, 如果用通常的形式是不成功的. 比如下面的 “I” 出現(xiàn)在第二行開頭, 但是使用 r"^I" 卻匹配不到第二行, 這時(shí)候, 我們要使用 另外一個(gè)參數(shù), 讓 re.search() 可以對(duì)每一行單獨(dú)處理. 這個(gè)參數(shù)就是 flags=re.M, 或者這樣寫也行 flags=re.MULTILINE.

<ins data-ad-format="auto" class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-4601203457616636" data-adsbygoogle-status="done" data-overlap-observer-io="false" style="display: block; margin: auto; background-color: transparent;"><ins id="aswift_8_expand" style="display: inline-table; border: none; height: 159px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent;"><ins id="aswift_8_anchor" style="display: block; border: none; height: 159px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent;"><iframe id="aswift_8" name="aswift_8" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="636" height="159" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4601203457616636&output=html&h=159&adk=1744626125&adf=2663990526&w=636&lmt=1583377291&num_ads=1&rafmt=16&sem=mc&pwprc=9194589492&psa=0&guci=2.2.0.0.2.2.0.0&ad_type=text_image&format=636x159&url=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F13-10-regular-expression%2F&flash=0&pra=3&wgl=1&fa=27&adsid=NT&dt=1587802198495&bpp=2&bdt=897&idt=2&shv=r20200422&cbv=r20190131&ptt=9&saldr=aa&abxe=1&prev_fmts=700x90%2C250x250%2C636x159%2C636x159%2C300x250%2C0x0&nras=2&correlator=3466323590182&frm=20&pv=1&ga_vid=1212726724.1583946939&ga_sid=1587802198&ga_hid=1016610859&ga_fc=0&iag=0&icsg=9130715816&dssz=30&mdo=0&mso=0&u_tz=480&u_his=9&u_java=0&u_h=1080&u_w=1920&u_ah=1040&u_aw=1920&u_cd=24&u_nplug=3&u_nmime=4&adx=484&ady=4507&biw=1903&bih=937&scr_x=0&scr_y=0&eid=21065214&oid=3&pvsid=322963587975057&pem=821&ref=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F&rx=0&eae=0&fc=384&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1040%2C1920%2C937&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=8216&bc=31&ifi=8&uci=a!8&btvi=4&fsb=1&xpc=ZuJ21Wg7LZ&p=https%3A//morvanzhou.github.io&dtd=5" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!8" data-google-query-id="CK31vpCQg-kCFcEgKgodDV0EoQ" data-load-complete="true" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 636px; height: 159px;"></iframe></ins></ins></ins>

string = """
dog runs to cat.
I run to dog.
"""
print(re.search(r"^I", string))                 # None
print(re.search(r"^I", string, flags=re.M))     # <_sre.SRE_Match object; span=(18, 19), match='I'>

重復(fù)匹配

如果我們想讓某個(gè)規(guī)律被重復(fù)使用, 在正則里面也是可以實(shí)現(xiàn)的, 而且實(shí)現(xiàn)的方式還有很多. 具體可以分為這三種:

  • * : 重復(fù)零次或多次
  • + : 重復(fù)一次或多次
  • {n, m} : 重復(fù) n 至 m 次
  • {n} : 重復(fù) n 次

舉例如下:

# * : occur 0 or more times
print(re.search(r"ab*", "a"))             # <_sre.SRE_Match object; span=(0, 1), match='a'>
print(re.search(r"ab*", "abbbbb"))        # <_sre.SRE_Match object; span=(0, 6), match='abbbbb'>

# + : occur 1 or more times
print(re.search(r"ab+", "a"))             # None
print(re.search(r"ab+", "abbbbb"))        # <_sre.SRE_Match object; span=(0, 6), match='abbbbb'>

# {n, m} : occur n to m times
print(re.search(r"ab{2,10}", "a"))        # None
print(re.search(r"ab{2,10}", "abbbbb"))   # <_sre.SRE_Match object; span=(0, 6), match='abbbbb'>

分組

我們甚至可以為找到的內(nèi)容分組, 使用 () 能輕松實(shí)現(xiàn)這件事. 通過分組, 我們能輕松定位所找到的內(nèi)容. 比如在這個(gè) (\d+) 組里, 需要找到的是一些數(shù)字, 在 (.+) 這個(gè)組里, 我們會(huì)找到 “Date: “ 后面的所有內(nèi)容. 當(dāng)使用 match.group() 時(shí), 他會(huì)返回所有組里的內(nèi)容, 而如果給 .group(2) 里加一個(gè)數(shù), 它就能定位你需要返回哪個(gè)組里的信息.

match = re.search(r"(\d+), Date: (.+)", "ID: 021523, Date: Feb/12/2017")
print(match.group())                   # 021523, Date: Feb/12/2017
print(match.group(1))                  # 021523
print(match.group(2))                  # Date: Feb/12/2017

有時(shí)候, 組會(huì)很多, 光用數(shù)字可能比較難找到自己想要的組, 這時(shí)候, 如果有一個(gè)名字當(dāng)做索引, 會(huì)是一件很容易的事. 我們字需要在括號(hào)的開頭寫上這樣的形式 ?P<名字> 就給這個(gè)組定義了一個(gè)名字. 然后就能用這個(gè)名字找到這個(gè)組的內(nèi)容.

match = re.search(r"(?P<id>\d+), Date: (?P<date>.+)", "ID: 021523, Date: Feb/12/2017")
print(match.group('id'))                # 021523
print(match.group('date'))              # Date: Feb/12/2017

<ins class="adsbygoogle" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4601203457616636" data-ad-slot="3397817325" data-adsbygoogle-status="done" data-overlap-observer-io="false" style="display: block; text-align: center; height: 123px; width: 636px;"><ins id="aswift_4_expand" style="display: inline-table; border: none; height: 123px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent;"><ins id="aswift_4_anchor" style="display: block; border: none; height: 123px; margin: 0px; padding: 0px; position: relative; visibility: visible; width: 636px; background-color: transparent; overflow: hidden;"><iframe id="aswift_4" name="aswift_4" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="636" height="123" frameborder="0" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4601203457616636&output=html&h=159&slotname=3397817325&adk=3401585631&adf=2619620792&w=636&fwrn=4&lmt=1583377291&rafmt=11&psa=0&guci=2.2.0.0.2.2.0.0&format=636x159&url=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F13-10-regular-expression%2F&flash=0&wgl=1&adsid=NT&dt=1587802197991&bpp=2&bdt=393&idt=131&shv=r20200422&cbv=r20190131&ptt=9&saldr=aa&abxe=1&prev_fmts=700x90%2C250x250%2C636x159%2C636x159%2C300x250%2C0x0%2C636x159&nras=2&correlator=3466323590182&frm=20&pv=1&ga_vid=1212726724.1583946939&ga_sid=1587802198&ga_hid=1016610859&ga_fc=0&iag=0&icsg=43490454184&dssz=33&mdo=0&mso=0&rplot=4&u_tz=480&u_his=9&u_java=0&u_h=1080&u_w=1920&u_ah=1040&u_aw=1920&u_cd=24&u_nplug=3&u_nmime=4&adx=484&ady=6053&biw=1903&bih=937&scr_x=0&scr_y=2305&eid=21065214&oid=3&psts=AKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ%2CAKB7eCK4lOp292lEAVSqA7mQs41q8NeRpyYvav9UdXDUuHhp_SKIMiPoxfCs2yoHVOjnLQ&pvsid=322963587975057&pem=821&ref=https%3A%2F%2Fmorvanzhou.github.io%2Ftutorials%2Fpython-basic%2Fbasic%2F&rx=0&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1040%2C1920%2C937&vis=1&rsz=%7C%7CeEbr%7C&abl=CS&pfx=0&fu=8344&bc=31&ifi=5&uci=a!5&btvi=5&fsb=1&xpc=6Rn7ABKl1J&p=https%3A//morvanzhou.github.io&dtd=M" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" data-google-container-id="a!5" data-google-query-id="CK-2rcSQg-kCFYrXuwgdepIJ3w" data-load-complete="true" style="left: 0px; position: absolute; top: 0px; border: 0px; width: 636px; height: 123px;"></iframe></ins></ins></ins>

findall

前面我們說的都是只找到了最開始匹配上的一項(xiàng)而已, 如果需要找到全部的匹配項(xiàng), 我們可以使用 findall 功能. 然后返回一個(gè)列表. 注意下面還有一個(gè)新的知識(shí)點(diǎn), | 是 or 的意思, 要不是前者要不是后者.

# findall
print(re.findall(r"r[ua]n", "run ran ren"))    # ['run', 'ran']

# | : or
print(re.findall(r"(run|ran)", "run ran ren")) # ['run', 'ran']

replace

我們還能通過正則表達(dá)式匹配上一些形式的字符串然后再替代掉這些字符串. 使用這種匹配 re.sub(), 將會(huì)比 python 自帶的 string.replace() 要靈活多變.

print(re.sub(r"r[au]ns", "catches", "dog runs to cat"))     # dog catches to cat

split

再來我們 Python 中有個(gè)字符串的分割功能, 比如想獲取一句話中所有的單詞. 比如 "a is b".split(" "), 這樣它就會(huì)產(chǎn)生一個(gè)列表來保存所有單詞. 但是在正則中, 這種普通的分割也可以做的淋漓精致.

print(re.split(r"[,;\.]", "a;b,c.d;e"))             # ['a', 'b', 'c', 'd', 'e']

compile

最后, 我們還能使用 compile 過后的正則, 來對(duì)這個(gè)正則重復(fù)使用. 先將正則 compile 進(jìn)一個(gè)變量, 比如 compiled_re, 然后直接使用這個(gè) compiled_re 來搜索.

compiled_re = re.compile(r"r[ua]n")
print(compiled_re.search("dog ran to cat"))  # <_sre.SRE_Match object; span=(4, 7), match='ran'>

小抄

為了大家方便記憶, 我很久以前在網(wǎng)上找到了一份小抄, 這個(gè)小抄的原出處應(yīng)該是這里. 小抄很有用, 不記得的時(shí)候回頭方便看.


RegEx.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末痴荐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件是偷,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)沃斤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刃宵,“玉大人衡瓶,你說我怎么就攤上這事∩ぃ” “怎么了哮针?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)坦袍。 經(jīng)常有香客問我十厢,道長(zhǎng),這世上最難降的妖魔是什么捂齐? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任蛮放,我火速辦了婚禮,結(jié)果婚禮上奠宜,老公的妹妹穿的比我還像新娘包颁。我一直安慰自己瞻想,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布娩嚼。 她就那樣靜靜地躺著蘑险,像睡著了一般。 火紅的嫁衣襯著肌膚如雪岳悟。 梳的紋絲不亂的頭發(fā)上漠其,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音竿音,去河邊找鬼和屎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛春瞬,可吹牛的內(nèi)容都是我干的柴信。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宽气,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼随常!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起萄涯,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤绪氛,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后涝影,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枣察,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年燃逻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了序目。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡伯襟,死狀恐怖猿涨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姆怪,我是刑警寧澤叛赚,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站稽揭,受9級(jí)特大地震影響俺附,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜淀衣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一昙读、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧膨桥,春花似錦蛮浑、人聲如沸唠叛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)艺沼。三九已至,卻和暖如春蕴掏,著一層夾襖步出監(jiān)牢的瞬間障般,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工盛杰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挽荡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓即供,卻偏偏與公主長(zhǎng)得像定拟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子逗嫡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • TRY REGEX 是一個(gè)交互式的正則表達(dá)式學(xué)習(xí)項(xiàng)目項(xiàng)目地址:https://github.com/calluma...
    蘿卜粥_Carrot閱讀 532評(píng)論 0 0
  • 搞懂Python 正則表達(dá)式用法 Python 正則表達(dá)式 正則表達(dá)式是一個(gè)特殊的字符序列青自,它能幫助你方便的檢查一...
    廈熱閱讀 1,580評(píng)論 0 2
  • 注:本篇文章只為方便查看,特此保留驱证,如有冒犯延窜,敬請(qǐng)諒解!Dǔ逆瑞! 本文目標(biāo) 30分鐘內(nèi)讓你明白正則表達(dá)式是什么,并對(duì)它...
    阿杰Alex閱讀 1,483評(píng)論 0 10
  • 前天跟朋友們聚會(huì)祈远,席間突然被郭姐的手吸引了呆万,哇商源,好炫啊车份,各種珠寶戒指戴滿雙手,黃的綠的紅的貓眼的水晶的蜜蠟的琳瑯滿...
    jiuli閱讀 277評(píng)論 0 0
  • 春天來了 感覺體內(nèi)的洪荒之力 蠢蠢欲動(dòng) 腦袋有無窮的問題 蓬勃待發(fā) 我在想 人生除了寫代碼 看書 吃喝玩樂 還應(yīng)該...
    光劍書架上的書閱讀 217評(píng)論 0 3