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

Python正則表達式一文詳解+實例代碼展示

編輯:Python

目錄

前言

一、正則表達式

1.簡介

2.概念

3.目的

4.特點

二、Re庫

1.re.match()

?2.fullmatch()

?3.search()

4.?sub()

?5.subn()

6.findall()

?7.finditer()

?8.compile()

9.?splite()

?10.Match對象和Pattern對象

三.正則表達式語法匹配

1 普通字符

1.1alone

?1.2[alone]

1.3[^alone]?

1.4[A-Z]

1.5 .

?1.6[sS]

1.7w

?2.非打印字符

2.1cx

2.2

2.3

2.4

2.5?s

?2.6S

?2.7

2.8

3.特殊字符

3.1$

?3.2( )

?3.3*

?3.4+

?3.5.

?3.6[

3.7?

3.8

3.9^

?3.10{

3.11|

4.限定符

4.1*

4.2+

4.3?

4.4{n}

4.5?{n,}

4.6?{n,m}

5.定位符

5.1^

5.2$

5.3

?5.4B

點關注,防走丟,如有纰漏之處,請留言指教,非常感謝

參閱:


很多學Python伙伴問題有沒有體系的面試題?
今天為大家推薦一款刷題神奇哦 :刷題面試神器牛客
各大互聯網大廠面試真題。從基礎到入階乃至原理刨析類面試題 應有盡有,趕快來裝備自己吧!助你面試穩操勝券,solo全場面試官


前言

正則匹配可以說是數據檢索和遍歷的基礎技能了。在Python我們經常會用到需要提出特定字符的場景,尤其在網絡爬蟲爬取的編碼後雜亂網頁提取數據的的時候。在數據庫提取數據後或是JSON文件做二次處理都需要re庫進行提取。因此學好re庫和正則表達是python工程師的必修課,你可以對正則表達式掌握的不是很熟練,但是必須要了解re庫中存在的各種函數功能,這將大大加速你完成項目某項需求的時間也方便進行問題搜索。本文將詳細介紹Python中正則表達式功能re庫的功能。


一、正則表達式

1.簡介

正則表達式,又稱規則表達式**,**(Regular Expression,在代碼中常簡寫為regex、regexp或RE),是一種文本模式,包括普通字符(例如,a 到 z 之間的字母)和特殊字符(稱為"元字符"),是計算機科學的一個概念。正則表達式是對字符串(包括普通字符(例如,a 到 z 之間的字母)和特殊字符(稱為“元字符”))操作的一種邏輯公式,就是用事先定義好的一些特定字符及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。正則表達式是一種文本模式,該模式描述在搜索文本時要匹配的一個或多個字符串。正則表達式使用單個字符串來描述、匹配一系列匹配某個句法規則的字符串,通常被用來檢索、替換那些符合某個模式(規則)的文本。

許多程序設計語言都支持利用正則表達式進行字符串操作。例如,在Perl中就內建了一個功能強大的正則表達式引擎。正則表達式這個概念最初是由Unix中的工具軟件(例如sed和grep)普及開來的,後來在廣泛運用於Scala 、PHP、C# 、Java、C++ 、Objective-c、Perl 、Swift、VBScript 、Javascript、Ruby 以及Python等等。正則表達式通常縮寫成“regex”,單數有regexp、regex,復數有regexps、regexes、regexen。

2.概念

正則表達式是對字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個“規則字符串”,這個“規則字符串”用來表達對字符串的一種過濾邏輯。

3.目的

給定一個正則表達式和另一個字符串,我們可以達到如下的目的:

  • 判斷給定的字符串是否符合正則表達式的過濾邏輯(稱作“匹配”):
  • 可以通過正則表達式,從字符串中獲取我們想要的特定部分。

4.特點

正則表達式的特點是:

  • 靈活性、邏輯性和功能性非常強;
  • 可以迅速地用極簡單的方式達到字符串的復雜控制。
  • 對於剛接觸的人來說,比較晦澀難懂。

二、Re庫

re庫是Python自帶的標准庫,不需要安裝即可使用:

Re庫默認采用貪婪匹配,即輸出匹配最長的子串

import re

模塊常用函數:

1.re.match()

基礎語法形式為:

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

函數功能為 :從一個字符串的開始位置起匹配正則表達式,返回match對象。如果開始沒有匹配到,不管後邊有沒有能符合匹配的,都打印不出結果,這是和search的區別。

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • flags:正則表達式使用時的控制標記

其中flags可選參數有:

參數

說明

re.l/re.IGNORECASE

忽略大小寫

re.M/re.MULTILINE

多行模式,改變‘^‘和’$'的行為

re.S/re.DOTALL

點’.'的任意匹配模式,改變’.‘的行為

re.L/re.LOCALE

使預定字符類wWBsS取決於當前區域設定

re.U/re.UNICODE

使預定字符類wWBsSdD取決於unicode定義的字符屬性

re.X/re.VERBOSE

詳細模式。這個模式下正則表達式可以是多行,忽略空白字符,並且可以加入注釋

我們把復雜的匹配放到第三章再寫,這裡我們僅簡單展示一下該函數的用法和效果:

strings='Fanstuck wants to leave alone'
print(re.match('Fanstuck',strings))
#out: <re.Match object; span=(0, 8), match='Fanstuck'>

其中span就匹配到的pattern在字符串中的位置。

strings='Fanstuck wants to leave alone'
print(re.match('anstuck',strings))
#out: None

2.fullmatch()

基礎語法格式為:

fullmatch(pattern, string, flags=0)

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • flags:正則表達式使用時的控制標記

函數功能為 :嘗試將正則表達式pattern應用於所有字符串string,如果匹配成功,返回匹配對象;如果找不到匹配,則為“None”。

用法效果展示:

strings='Fanstuck wants to leave alone'
print(re.fullmatch('Fanstuck wants to leave alone',strings))
#out: <re.Match object; span=(0, 29), match='Fanstuck wants to leave alone'>

該函數為貪婪規則指定的pattern都必須完全和strings長度和字符一一對應,否則就是None:

strings='Fanstuck wants to leave alone'
print(re.fullmatch('Fanstuck wants to leave alon',strings))
#out: None

3.search()

基礎語法格式為:

search(pattern, string, flags=0)

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • flags:正則表達式使用時的控制標記

函數功能為 :掃描字符串string以查找與正則表達式pattern的匹配項,返回匹配對象;如果找不到匹配,則為“None”。re.search 匹配整個字符串,直到找到一個匹配。

strings='Fanstuck wants to leave alone'
print(re.search('alone',strings))
#out:<re.Match object; span=(24, 29), match='alone'>
strings='Fanstuck wants to leave alone'
print(re.search('die',strings))
#out:None

4.sub()

基本語法格式為:

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

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示

  • repl:將匹配到的pattern替換為repl

  • string:匹配的字符串

  • count:模式匹配後替換的最大次數,默認 0 表示替換所有的匹配

  • flags:正則表達式使用時的控制標記

    strings=‘Fanstuck wants to leave alone alonely’
    print(re.sub(‘leave’,‘die’,strings))
    #out:Fanstuck wants to die alone alonely

    strings=‘Fanstuck wants to leave alone alonely’
    print(re.sub(‘alone’,‘sad’,strings))
    #out:Fanstuck wants to leave sad sadly

5.subn()

基礎語法格式為:

subn(pattern, repl, string, count=0, flags=0)

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • repl:將匹配到的pattern替換為repl
  • string:匹配的字符串
  • count:模式匹配後替換的最大次數,默認 0 表示替換所有的匹配
  • flags:正則表達式使用時的控制標記

與前一個函數sub相比,無非就是增加了一個次數:

strings='Fanstuck wants to leave alone alonely'
print(re.subn('alone','sad',strings))
#out:('Fanstuck wants to leave sad sadly', 2)

但是轉化為字典dict或者是用於pandas都是比較方便的,不用在統計出匹配了多少個字段。

6.findall()

基礎語法格式為:

findall(pattern, string, flags=0)
或
findall(pattern,string, pos, endpos)

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • flags:正則表達式使用時的控制標記
  • pos:可選參數,指定字符串的起始位置,默認為 0。
  • endpos:可選參數,指定字符串的結束位置,默認為字符串的長度

函數功能為 :在字符串string中匹配所有符合正則表達式pattern的對象,並把這些對象通過列表list的形式返回。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('alone',strings))
#out:['alone', 'alone']
strings='Fanstuck wants to leave alone alonely'
print(re.findall('alonely',strings))
#out:['alonely']
strings='Fanstuck wants to leave alone alonely'
pattern=re.compile('a')
print(pattern.findall(strings,0,30))

7.finditer()

基礎語法格式為:

finditer(pattern, string, flags=0)

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • flags:正則表達式使用時的控制標記

函數功能為:在字符串string中匹配所有符合正則表達式pattern的對象,並把這些對象通過迭代器的形式返回。

strings='Fanstuck wants to leave alone alonely'
result=re.finditer('alone',strings)
for i in result:
print(i)
#out:<re.Match object; span=(24, 29), match='alone'>
#out:<re.Match object; span=(30, 35), match='alone'>

8.compile()

基礎語法格式為:

compile(pattern, flags=0)
  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • flags:正則表達式使用時的控制標記

compile 函數用於編譯正則表達式,生成一個正則表達式( Pattern )對象,供 match() 和 search() 這兩個函數使用。

strings='Fanstuck wants to leave alone alonely'
pattern=re.compile('to')
pattern.search(strings)
#out:<re.Match object; span=(15, 17), match='to'>
strings='Fanstuck wants to leave alone alonely'
pattern=re.compile('to')
object_search=pattern.search(strings)
object_search.group()
#out:'to'
object_search.start()
#out:15
object_search.end()
#out:17
object_search.span()
#out:(15,17)

9.splite()

基礎語法格式為:

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

參數說明:

  • pattern:匹配目標對象的正則表達式pattern或原生字符串表示
  • string:匹配的字符串
  • maxsplit:分隔次數,maxsplit=1 分隔一次,默認為 0,不限制次數
  • flags:正則表達式使用時的控制標記

pattern匹配的子串來分割string,如果pattern裡使用了圓括號,那麼被pattern匹配到的串也將作為返回值列表的一部分,maxsplit為最多被分割的字符串個數。

strings='Fanstuck wants to leave alone alonely'
re.split(r' ', strings)
#out:['Fanstuck', 'wants', 'to', 'leave', 'alone', 'alonely']
strings='Fanstuck wants to leave alone alonely'
re.split(r' ', strings,maxsplit=2)
#out:['Fanstuck', 'wants', 'to leave alone alonely']
strings='Fanstuck wants to leave alone alonely'
re.split(r'( )', strings,maxsplit=2)
#out:['Fanstuck', ' ', 'wants', ' ', 'to leave alone alonely']

10.Match對象和Pattern對象

re.match()、re.search()成功匹配的話都會返回一個Match對象,它包含了關於此次匹配的信息,可以使用Match提供的屬性或方法來獲取這些信息;Pattern對象對象由re.compile()生成,而且方法用處也是一樣的。

strings='Fanstuck wants to leave alone alonely'
pattern=re.compile('to')
object_search=pattern.search(strings)
object_search.string
#out:'Fanstuck wants to leave alone alonely'
object_search.re
#out:re.compile(r'to', re.UNICODE)
object_search.pos
#out:0 (開始匹配的位置)
object_search.endpos
#out:37(結束匹配的位置)
object_search.lastindex
#out:None
object_search.lastgroup
#out:None
object_search.groupdict()
#out:{}
object_search.group()
#out:'to'
object_search.start()
#out:15
object_search.end()
#out:17
object_search.span()
#out:(15,17)

三.正則表達式語法匹配

正則表達式(regular expression)描述了一種字符串匹配的模式(pattern),可以用來檢查一個串是否含有某種子串、將匹配的子串替換或者從某個串中取出符合某個條件的子串等。

待會我們會用到re庫中的函數還進行字符匹配,這裡可以通過一個例子先看一下語法:

import re
a = "abbbbbccccd"
b = re.findall('ab+c+d',a)
print(b)

[‘abbbbbccccd’]

構造正則表達式的方法和創建數學表達式的方法一樣。也就是用多種元字符與運算符可以將小的表達式結合在一起來創建更大的表達式。正則表達式的組件可以是單個的字符、字符集合、字符范圍、字符間的選擇或者所有這些組件的任意組合。

正則表達式是由普通字符(例如字符 a 到 z)以及特殊字符(稱為"元字符")組成的文字模式。模式描述在搜索文本時要匹配的一個或多個字符串。正則表達式作為一個模板,將某個字符模式與所搜索的字符串進行匹配。

1 普通字符

普通字符包括沒有顯式指定為元字符的所有可打印和不可打印字符。這包括所有大寫和小寫字母、所有數字、所有標點符號和一些其他符號。

1.1alone

普通字符串,上述例子基本用的都是普通字符串。這裡我們用findall()函數來演示更好:

strings='Fanstuck wants to leave alone alonely'
print(re.findall('alone',strings))

[‘alone’, ‘alone’]

1.2[alone]

匹配[…]中的所有字符:

strings='Fanstuck wants to leave alone alonely'
print(re.findall('[alone]',strings))

[‘a’, ‘n’, ‘a’, ‘n’, ‘o’, ‘l’, ‘e’, ‘a’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’]

1.3[^alone]

匹配除了[^…]中字符的所有字符

strings='Fanstuck wants to leave alone alonely'
print(re.findall('[^alone]',strings))

[‘F’, ‘u’, ‘c’, ‘k’, ’ ', ’ ', ‘o’, ’ ', ‘l’, ‘e’, ‘v’, ‘e’, ’ ', ‘l’, ‘o’, ‘e’, ’ ', ‘l’, ‘o’, ‘e’, ‘l’, ‘y’]

1.4[A-Z]

[A-Z] 表示一個區間,匹配所有大寫字母,[a-z] 表示所有小寫字母。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('[^A-F]',strings))

[‘a’, ‘n’, ‘s’, ‘t’, ‘u’, ‘c’, ‘k’, ’ ', ‘w’, ‘a’, ‘n’, ‘t’, ‘s’, ’ ', ‘t’, ‘o’, ’ ', ‘l’, ‘e’, ‘a’, ‘v’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’, ‘y’]

1.5 .

匹配除換行符( 、 )之外的任何單個字符,相等於 [^ ]。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('.',strings))

[‘F’, ‘a’, ‘n’, ‘s’, ‘t’, ‘u’, ‘c’, ‘k’, ’ ', ‘w’, ‘a’, ‘n’, ‘t’, ‘s’, ’ ', ‘t’, ‘o’, ’ ', ‘l’, ‘e’, ‘a’, ‘v’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’, ‘y’]

1.6[sS]

匹配所有。s 是匹配所有空白符,包括換行,S 非空白符,不包括換行。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('[sS]',strings))

[‘F’, ‘a’, ‘n’, ‘s’, ‘t’, ‘u’, ‘c’, ‘k’, ’ ', ‘w’, ‘a’, ‘n’, ‘t’, ‘s’, ’ ', ‘t’, ‘o’, ’ ', ‘l’, ‘e’, ‘a’, ‘v’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ’ ', ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’, ‘y’]

1.7w

匹配字母、數字、下劃線。等價於 [A-Za-z0-9_]

strings='Fanstuck wants to leave alone alonely'
print(re.findall('w',strings))

[‘F’, ‘a’, ‘n’, ‘s’, ‘t’, ‘u’, ‘c’, ‘k’, ‘w’, ‘a’, ‘n’, ‘t’, ‘s’, ‘t’, ‘o’, ‘l’, ‘e’, ‘a’, ‘v’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’, ‘y’]

2.非打印字符

非打印字符也可以是正則表達式的組成部分。下表列出了表示非打印字符的轉義序列:

2.1cx

匹配由x指明的控制字符。例如, cM 匹配一個 Control-M 或回車符。x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一個原義的 ‘c’ 字符。

2.2

匹配一個換頁符。等價於 和 cL。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('',strings))

2.3

匹配一個換行符。等價於 和 cJ。

strings='Fanstuck
wants to leave
alone alonely'
print(re.findall('
',strings))
print(strings)

[’
']
Fanstuck
wants to leave alone alonely

2.4

匹配一個回車符。等價於 和 cM。

strings='
Fanstuck
wants to leave
alone alonely'
print(re.findall('
',strings))
print(strings)

[’
', ’
', ’
']
alone alonelye

2.5s

匹配任何空白字符,包括空格、制表符、換頁符等等。等價於 [ ]。注意 Unicode 正則表達式會匹配全角空格符。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('s',strings))

[’ ', ’ ', ’ ', ’ ', ’ ']

2.6S

匹配任何非空白字符。等價於 [^ ]。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('S',strings))

[‘F’, ‘a’, ‘n’, ‘s’, ‘t’, ‘u’, ‘c’, ‘k’, ‘w’, ‘a’, ‘n’, ‘t’, ‘s’, ‘t’, ‘o’, ‘l’, ‘e’, ‘a’, ‘v’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘a’, ‘l’, ‘o’, ‘n’, ‘e’, ‘l’, ‘y’]

2.7

匹配一個制表符。等價於 和 cI。

strings='Fanstuck wants to leave alone alonely'
print(re.findall(' ',strings))

沒寫啥制表符固為空。

2.8

匹配一個垂直制表符。等價於 和 cK。

3.特殊字符

所謂特殊字符,就是一些有特殊含義的字符,如上面說的runoo*b中的*,簡單的說就是表示任何字符串的意思。如果要查找字符串中的*符號,則需要對*進行轉義,即在其前加一個,runo*ob匹配字符串runo*ob

許多元字符要求在試圖匹配它們時特別對待。若要匹配這些特殊字符,必須首先使字符"轉義",即,將反斜槓字符放在它們前面。下表列出了正則表達式中的特殊字符:

3.1$

匹配輸入字符串的結尾位置。如果設置了 RegExp 對象的 Multiline 屬性,$ 還會與 或 之前的位置匹配。則 $ 也匹配 ’ ’ 或 ’ '。要匹配 $ 字符本身,請使用 $。

strings='Fanstuck wants to leave alone alonely'
print(re.findall(' alone alonely$',strings))

[’ alone alonely’]

3.2( )

標記一個子表達式的開始和結束位置。子表達式可以獲取供以後使用。要匹配這些字符,請使用 ( 和 )。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('(Fanw{2,3}ck)',strings))

[‘Fanstuck’]

3.3*

匹配前面的子表達式零次或多次。要匹配 * 字符,請使用 *。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('(Fanw*ck)',strings))

[‘Fanstuck’]

3.4+

匹配前面的子表達式一次或多次。要匹配 + 字符,請使用 +。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('(alone)+',strings))

[‘alone’, ‘alone’]

3.5.

匹配除換行符 之外的任何單字符。要匹配 . ,請使用 . 。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('Fa.s.u.k',strings))

[‘Fanstuck’]

3.6[

標記一個中括號表達式的開始。要匹配 [,請使用 [。

3.7

匹配前面的子表達式零次或一次,或指明一個非貪婪限定符。要匹配 字符,請使用 。

這裡要注意一下貪婪模式和非貪婪模式。

貪婪模式:盡可能多的去匹配數據,表現為w後面加某個元字符,例如w*:

strings='Fanstuck wants to leave alone alonely'
print(re.findall('Fw*',strings))

[‘Fanstuck’]

非貪婪模式:盡可能少的去匹配數據,表現為w後面加?,例如w

strings='Fanstuck wants to leave alone alonely'
print(re.findall('Fw?',strings))

[‘Fa’]

3.8

將下一個字符標記為或特殊字符、或原義字符、或向後引用、或八進制轉義符。例如, ‘n’ 匹配字符 ‘n’。’ ’ 匹配換行符。序列 ‘\’ 匹配 “”,而 ‘(’ 則匹配 “(”。

3.9^

strings='alone alonely'
print(re.findall('^alone',strings))

[‘alone’]

3.10{

標記限定符表達式的開始。要匹配 {,請使用 {。

3.11|

指明兩項之間的一個選擇。要匹配 |,請使用 |。

4.限定符

限定符用來指定正則表達式的一個給定組件必須要出現多少次才能滿足匹配。有*或+或或{n}或{n,}或{n,m}共6種。

正則表達式的限定符有:

4.1*

上述已作展示

4.2+

上述已作展示

4.3?

上述已作展示

4.4{n}

n 是一個非負整數。匹配確定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的兩個 o。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('Fanw{2}uck',strings))

4.5{n,}

n 是一個非負整數。至少匹配n 次。貪婪

strings='Fanstuck wants to leave alone alonely'
print(re.findall('Fanw{1,}uck',strings))

[‘Fanstuck’]

4.6{n,m}

m 和 n 均為非負整數,其中n <= m。最少匹配 n 次且最多匹配 m 次。貪婪

strings='Fanstuck wants to leave alone alonely'
print(re.findall('^(w{2,8}s*w{2,8})+',strings))a

[‘Fanstuck wants’]

strings='Fanstuck wants to leave alone alonely'
print(re.findall('^Fanstuck[(w{2,8}s*)+]+ly',strings))

[‘Fanstuck wants to leave alone alonely’]

*和+限定符都是貪婪的,因為它們會盡可能多的匹配文字,只有在它們的後面加上一個 就可以實現非貪婪或最小匹配。

5.定位符

5.1^

上述已經演示

5.2$

上述已經演示

5.3

匹配一個單詞邊界,即字與空格間的位置。

strings='Fanstuck wants to leave alone alonely'
print(re.findall(r'alone',strings))

[‘alone’]

5.4B

和’/b’相反,它只匹配非邊界的字符。

strings='Fanstuck wants to leave alone alonely'
print(re.findall('alone\Bly',strings))

[‘alonely’]

很多學Python伙伴問題有沒有體系的面試題?
今天為大家推薦一款刷題神奇哦 :刷題面試神器牛客
各大互聯網大廠面試真題。從基礎到入階乃至原理刨析類面試題 應有盡有,趕快來裝備自己吧!助你面試穩操勝券,solo全場面試官


點關注,防走丟,如有纰漏之處,請留言指教,非常感謝

以上就是本期全部內容。我是 ,有問題大家隨時留言討論 ,我們下期見。

參閱:

Python標准庫筆記(2) — re模塊

python正則表達式:re庫的使用

python庫詳解

re庫

python | 史上最全的正則表達式


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