上一篇文章: Python實用技法第24篇:正則:查找和替換文本
下一篇文章: Python實用技法第26篇:定義實現最短匹配的正則表達式
咱們須要以不區分大小寫的方式在文本中進行查找,可能還須要作替換。
要進行不區分大小寫的文本操做,咱們須要使用re模塊而且對各類操做都要加上re.IGNORECASE標記。python
示例:正則表達式
import re text='Mark is a handsome guy and mark is only 18 years old.' result1=re.findall('mark',text,flags=re.IGNORECASE) result2=re.sub('mark','python',text,flags=re.IGNORECASE) print(result1) print(result2)
結果:segmentfault
['Mark', 'mark'] python is a handsome guy and python is only 18 years old.
上面例子揭示了一種侷限,就是雖然名字從【mark】替換爲【python】,可是大小寫並不吻合,例如第一我的名替換後應該也是大寫:【Pyhton】。api
若是想要修正這個問題,須要用到一個支撐函數,實例以下:函數
import re text='Mark is a handsome guy and mark is only 18 years old.MARK' def matchcase(word): def replace(m): #re.sub會將匹配到的對象,循環調用replace方法傳入 print(m) #獲取匹配的文本 text=m.group() if text.isupper(): #若是文本所有是大寫,就返回word的所有大寫模式 return word.upper() elif text.islower(): # 若是文本所有是小寫,就返回word的所有小寫模式 return word.lower() elif text[0].isupper(): #若是文本是首字母大寫,就返回word的首字母大寫模式 return word.capitalize() else: #其餘狀況,直接返回word return word return replace result=re.sub('mark',matchcase('python'),text,flags=re.IGNORECASE) print(result)
運行結果:spa
<re.Match object; span=(0, 4), match='Mark'> <re.Match object; span=(27, 31), match='mark'> <re.Match object; span=(53, 57), match='MARK'> Python is a handsome guy and python is only 18 years old.PYTHON
對於簡單的狀況,只需加上re.IGNORECASE標記足以進行不區分大小寫的匹配操做了。code
但請注意,對於某些涉及大寫轉換的Unicode匹配來講多是不夠的,之後章節會講到。對象
上一篇文章: Python實用技法第24篇:正則:查找和替換文本
下一篇文章: Python實用技法第26篇:定義實現最短匹配的正則表達式