大慶油田app下載安裝關(guān)鍵詞seo排名怎么做的
?目錄
?專欄導(dǎo)讀?
1?正則表達式概述
2 正則表達式語法
2.1?正則表達式語法元素
?2.2 正則表達式的分組操作
?3 re 模塊詳解與示例
4 正則表達式修飾符
專欄導(dǎo)讀?
專欄訂閱地址:https://blog.csdn.net/qq_35831906/category_12375510.html
1?正則表達式概述
????????python 的正則表達式是什么,有哪些內(nèi)容,有什么功能,怎么用?
????????Python的正則表達式是一種用于處理字符串的強大工具,由re
模塊提供支持。正則表達式允許你根據(jù)特定模式來匹配、搜索、替換和提取文本數(shù)據(jù)。
正則表達式的基本組成包括:
- 字面字符:普通的字符,例如'a'、'b'等,它們直接匹配相應(yīng)的字符。
- 元字符:具有特殊含義的字符,例如'.'匹配任意字符、'\d'匹配數(shù)字等。
- 限定符:用于指定模式的匹配次數(shù),例如'*'匹配0次或多次、'+'匹配1次或多次等。
- 字符類:用于匹配一組字符中的任意一個字符,例如'[abc]'匹配'a'、'b'或'c'。
- 排除字符:在字符類中使用'^'來排除指定的字符。
- 轉(zhuǎn)義字符:用于匹配特殊字符本身,例如使用'.'匹配實際的點號。
正則表達式在文本處理中有很多功能:
- 模式匹配:查找字符串中是否包含特定的模式。
- 文本搜索:在字符串中搜索匹配模式的第一個出現(xiàn)。
- 查找所有:查找字符串中所有匹配模式的出現(xiàn),并返回所有匹配結(jié)果的列表。
- 分割:根據(jù)模式將字符串分割成多個部分。
- 替換:將匹配模式的部分替換為指定的字符串。
以下是一個簡單的使用正則表達式的示例:
import repattern = r'\d+' # 匹配一個或多個數(shù)字
text = "There are 123 apples and 456 oranges."# 搜索
search_result = re.search(pattern, text)
if search_result:print("Found:", search_result.group())# 查找所有
findall_result = re.findall(pattern, text)
print(findall_result) # Output: ['123', '456']
????????上述代碼中,
re.search()
函數(shù)搜索第一個匹配的數(shù)字,而re.findall()
函數(shù)查找字符串中所有匹配的數(shù)字。????????使用正則表達式時,應(yīng)當(dāng)確保模式能夠正確匹配目標文本,同時注意處理可能出現(xiàn)的異常情況。熟練掌握正則表達式,可以在文本處理中實現(xiàn)高效和靈活的匹配、搜索和替換操作
2 正則表達式語法
2.1?正則表達式語法元素
????行定位符、元字符、限定符、字符類、排除字符、選擇字符和轉(zhuǎn)義字符是正則表達式的基本組成部分,它們用于描述和匹配字符串的模式。
行定位符:
"^"
:匹配字符串的開頭。"$"
:匹配字符串的結(jié)尾。元字符:
"."
:匹配任意字符(除了換行符)。"\d"
:匹配任意數(shù)字字符,等同于[0-9]
。"\D"
:匹配任意非數(shù)字字符,等同于[^0-9]
。"\w"
:匹配任意字母、數(shù)字或下劃線字符,等同于[a-zA-Z0-9_]
。"\W"
:匹配任意非字母、數(shù)字或下劃線字符,等同于[^a-zA-Z0-9_]
。"\s"
:匹配任意空白字符,包括空格、制表符、換行符等。"\S"
:匹配任意非空白字符。限定符:
"*"
:匹配前一個字符零次或多次。"+"
:匹配前一個字符一次或多次。"?"
:匹配前一個字符零次或一次。"{n}"
:匹配前一個字符恰好n次。"{n,}"
:匹配前一個字符至少n次。"{n, m}"
:匹配前一個字符至少n次,但不超過m次。字符類:
"[...]"
:匹配方括號內(nèi)的任意一個字符。"[^...]"
:匹配除方括號內(nèi)的字符之外的任意一個字符。排除字符:
"^"
:在字符類內(nèi)使用,表示排除指定字符。選擇字符:
"|"
:邏輯或,匹配兩個模式之一。轉(zhuǎn)義字符:
"\"
:用于轉(zhuǎn)義特殊字符,使其失去特殊含義,例如\.
匹配實際的點號????????這些元字符和特殊符號組合形成了正則表達式的模式,使得正則表達式可以描述非常復(fù)雜的字符串匹配規(guī)則。要使用正則表達式,你可以使用Python的
re
模塊提供的函數(shù)進行匹配、搜索、替換等操作。熟悉這些基本元素有助于編寫更加強大和靈活的正則表達式。
?示例:
import re# 行定位符
pattern1 = r'^Hello' # 匹配以"Hello"開頭的字符串
print(re.match(pattern1, "Hello, World!")) # Output: <re.Match object; span=(0, 5), match='Hello'>pattern2 = r'World$' # 匹配以"World"結(jié)尾的字符串
print(re.search(pattern2, "Hello, World!")) # Output: <re.Match object; span=(7, 12), match='World'># 元字符
pattern3 = r'a.c' # 匹配"a"、任意字符、"c"
print(re.search(pattern3, "abc")) # Output: <re.Match object; span=(0, 3), match='abc'>
print(re.search(pattern3, "adc")) # Output: <re.Match object; span=(0, 3), match='adc'>
print(re.search(pattern3, "a,c")) # Output: <re.Match object; span=(0, 3), match='a,c'>pattern4 = r'ab*' # 匹配"a"、"b"出現(xiàn)0次或多次
print(re.search(pattern4, "abbb")) # Output: <re.Match object; span=(0, 1), match='a'>
print(re.search(pattern4, "ac")) # Output: <re.Match object; span=(0, 0), match=''>pattern5 = r'ab+' # 匹配"a"、"b"出現(xiàn)1次或多次
print(re.search(pattern5, "abbb")) # Output: <re.Match object; span=(0, 4), match='abbb'>
print(re.search(pattern5, "ac")) # Output: Nonepattern6 = r'ab?' # 匹配"a"、"b"出現(xiàn)0次或1次
print(re.search(pattern6, "abbb")) # Output: <re.Match object; span=(0, 1), match='a'>
print(re.search(pattern6, "ac")) # Output: <re.Match object; span=(0, 0), match=''># 限定符
pattern7 = r'a{3}' # 匹配"a"出現(xiàn)3次
print(re.search(pattern7, "aaa")) # Output: <re.Match object; span=(0, 3), match='aaa'>
print(re.search(pattern7, "aaaa")) # Output: <re.Match object; span=(0, 3), match='aaa'>
print(re.search(pattern7, "aa")) # Output: Nonepattern8 = r'a{3,5}' # 匹配"a"出現(xiàn)3次到5次
print(re.search(pattern8, "aaa")) # Output: <re.Match object; span=(0, 3), match='aaa'>
print(re.search(pattern8, "aaaaa")) # Output: <re.Match object; span=(0, 5), match='aaaaa'>
print(re.search(pattern8, "aaaaaa")) # Output: <re.Match object; span=(0, 5), match='aaaaa'># 字符類和排除字符
pattern9 = r'[aeiou]' # 匹配任意一個小寫元音字母
print(re.search(pattern9, "apple")) # Output: <re.Match object; span=(0, 1), match='a'>
print(re.search(pattern9, "banana")) # Output: <re.Match object; span=(1, 2), match='a'>
print(re.search(pattern9, "xyz")) # Output: Nonepattern10 = r'[^0-9]' # 匹配任意一個非數(shù)字字符
print(re.search(pattern10, "hello")) # Output: <re.Match object; span=(0, 1), match='h'>
print(re.search(pattern10, "123")) # Output: None# 轉(zhuǎn)義字符
pattern11 = r'\.' # 匹配句號
print(re.search(pattern11, "www.example.com")) # Output: <re.Match object; span=(3, 4), match='.'># 分組
pattern12 = r'(ab)+' # 匹配"ab"出現(xiàn)1次或多次作為一個整體
print(re.search(pattern12, "ababab")) # Output: <re.Match object; span=(0, 6), match='ababab'>
輸出結(jié)果顯示了匹配的子字符串的起始位置和結(jié)束位置,以及匹配的實際字符串內(nèi)容。
常用元字符
常用限定符??
?2.2 正則表達式的分組操作
? ? ? ? 在正則表達式中,分組是一種將多個子模式組合在一起并對其進行單獨處理的機制。通過使用括號()
來創(chuàng)建分組,可以實現(xiàn)更復(fù)雜的匹配和提取操作。
分組的作用包括:
-
優(yōu)先級控制:可以使用分組來改變子模式的優(yōu)先級,確保正確的匹配順序。
-
子模式重用:可以對某個子模式進行命名,并在后續(xù)的正則表達式中引用這個名稱,實現(xiàn)對同一模式的重用。
-
子模式提取:可以通過分組來提取匹配的子串,方便對其中的內(nèi)容進行進一步處理。
示例:
import retext = "John has 3 cats and Mary has 2 dogs."# 使用分組提取匹配的數(shù)字和動物名稱
pattern = r'(\d+)\s+(\w+)' # 使用括號創(chuàng)建兩個分組:一個用于匹配數(shù)字,另一個用于匹配動物名稱
matches = re.findall(pattern, text) # 查找所有匹配的結(jié)果并返回一個列表for match in matches:count, animal = match # 將匹配結(jié)果拆分為兩個部分:數(shù)字和動物名稱print(f"{count} {animal}")# 使用命名分組
pattern_with_name = r'(?P<Count>\d+)\s+(?P<Animal>\w+)' # 使用命名分組,給子模式指定名稱Count和Animal
matches_with_name = re.findall(pattern_with_name, text) # 查找所有匹配的結(jié)果并返回一個列表for match in matches_with_name:count = match['Count'] # 通過名稱獲取匹配結(jié)果中的數(shù)字部分animal = match['Animal'] # 通過名稱獲取匹配結(jié)果中的動物名稱部分print(f"{count} {animal}")
?以上代碼演示了如何使用分組提取正則表達式中匹配的子串。第一個正則表達式使用了普通分組,通過括號將數(shù)字和動物名稱分別提取出來。第二個正則表達式使用了命名分組,通過
(?P<Name>...)
的語法形式給子模式指定了名稱,從而在匹配結(jié)果中可以通過名稱獲取對應(yīng)的子串。這樣可以使代碼更具可讀性,方便后續(xù)對匹配結(jié)果的處理和使用。
上述代碼報錯如下
"TypeError: tuple indices must be integers or slices, not str" 這個錯誤意味著在代碼中嘗試使用字符串作為元組的索引,但元組的索引只能是整數(shù)或切片。
當(dāng)使用元組的時候,需要用整數(shù)或切片來獲取元組中的元素,如:
my_tuple[0]
或my_tuple[1:3]
,這些是合法的索引方式。但如果你嘗試使用字符串來索引元組中的元素,比如:my_tuple['key']
,這就是不合法的,因為元組并沒有與字符串索引相關(guān)聯(lián)的鍵值對。
更正:用?re.finditer()替代第二個?re.findall(),用match.group()
獲取匹配結(jié)果中的內(nèi)容。
更正后代碼:
import retext = "John has 3 cats and Mary has 2 dogs."# 使用分組提取匹配的數(shù)字和動物名稱
pattern = r'(\d+)\s+(\w+)' # 使用括號創(chuàng)建兩個分組:一個用于匹配數(shù)字,另一個用于匹配動物名稱
matches = re.findall(pattern, text) # 查找所有匹配的結(jié)果并返回一個列表for match in matches:count, animal = match # 將匹配結(jié)果拆分為兩個部分:數(shù)字和動物名稱print(f"{count} {animal}")# 使用命名分組
pattern_with_name = r'(?P<Count>\d+)\s+(?P<Animal>\w+)' # 使用命名分組,給子模式指定名稱Count和Animal
matches_with_name = re.finditer(pattern_with_name, text) # 使用re.finditer()查找所有匹配的結(jié)果for match in matches_with_name:count = match.group('Count') # 通過名稱獲取匹配結(jié)果中的數(shù)字部分animal = match.group('Animal') # 通過名稱獲取匹配結(jié)果中的動物名稱部分print(f"{count} {animal}")
注:?
?
re.findall()
和re.finditer()
都是Python中用于正則表達式匹配的函數(shù),它們的區(qū)別在于返回的結(jié)果類型不同。
re.findall(pattern, string)
:findall
函數(shù)會返回所有與正則表達式pattern
匹配的結(jié)果,并將它們以列表的形式返回。每個匹配結(jié)果將作為一個字符串元素存儲在列表中。如果正則表達式中有分組,findall
只會返回分組中的內(nèi)容而不返回完整的匹配結(jié)果。
re.finditer(pattern, string)
:finditer
函數(shù)也會返回所有與正則表達式pattern
匹配的結(jié)果,但不同于findall
,finditer
返回的是一個迭代器。每個迭代器對象代表一個匹配結(jié)果,可以通過迭代器的group()
方法來獲取匹配結(jié)果中的內(nèi)容。如果正則表達式中有分組,可以使用group()
方法來訪問各個分組的內(nèi)容。總結(jié)起來,
re.findall()
返回一個列表,而re.finditer()
返回一個迭代器。如果需要處理多個匹配結(jié)果,使用finditer
更加靈活和高效,因為它不會一次性返回所有匹配結(jié)果,而是在需要時按需提供。
?
3 re 模塊詳解與示例
? ?re
模塊是Python中用于處理正則表達式的內(nèi)置模塊,提供了一系列函數(shù)來進行字符串匹配、搜索、替換和分割等操作。以下是re
模塊的主要函數(shù):
re.compile(pattern, flags=0)
: 編譯正則表達式模式,返回一個正則表達式對象。如果要多次使用相同的正則表達式,可以使用這個函數(shù)預(yù)編譯,提高性能。
re.match(pattern, string, flags=0)
: 嘗試從字符串的開頭開始匹配模式,如果匹配成功,則返回匹配對象;否則返回None。
re.search(pattern, string, flags=0)
: 在整個字符串中搜索匹配模式的第一個出現(xiàn),如果匹配成功,則返回匹配對象;否則返回None。
re.findall(pattern, string, flags=0)
: 查找字符串中所有匹配模式的出現(xiàn),返回所有匹配結(jié)果的列表。
re.finditer(pattern, string, flags=0)
: 查找字符串中所有匹配模式的出現(xiàn),返回一個迭代器,可以通過迭代器獲取匹配對象。
re.split(pattern, string, maxsplit=0, flags=0)
: 根據(jù)模式將字符串分割成多個部分,并返回一個列表。
re.sub(pattern, replacement, string, count=0, flags=0)
: 將匹配模式的部分替換為指定的字符串,并返回替換后的字符串。
在上述函數(shù)中,pattern
是正則表達式的模式,string
是要進行匹配或處理的字符串,flags
是可選參數(shù),用于指定正則表達式的修飾符。其中,flags
參數(shù)可以使用多個修飾符進行組合,例如使用re.IGNORECASE | re.MULTILINE
來指定忽略大小寫和多行匹配。
以下示例展示了re
模塊中各種函數(shù)的使用,并涵蓋了匹配、搜索、替換、分割、命名分組等功能:
import retext = "John has 3 cats, Mary has 2 dogs."# 使用re.search()搜索匹配模式的第一個出現(xiàn)
pattern_search = r'\d+\s+\w+'
search_result = re.search(pattern_search, text)
if search_result:print("Search result:", search_result.group()) # Output: "3 cats"# 使用re.findall()查找所有匹配模式的出現(xiàn),并返回一個列表
pattern_findall = r'\d+'
findall_result = re.findall(pattern_findall, text)
print("Find all result:", findall_result) # Output: ['3', '2']# 使用re.sub()將匹配模式的部分替換為指定的字符串
pattern_sub = r'\d+'
replacement = "X"
sub_result = re.sub(pattern_sub, replacement, text)
print("Sub result:", sub_result) # Output: "John has X cats, Mary has X dogs."# 使用re.split()根據(jù)模式將字符串分割成多個部分
pattern_split = r'\s*,\s*' # 匹配逗號并去除前后空格
split_result = re.split(pattern_split, text)
print("Split result:", split_result) # Output: ['John has 3 cats', 'Mary has 2 dogs.']# 使用命名分組
pattern_named_group = r'(?P<Name>\w+)\s+has\s+(?P<Count>\d+)\s+(?P<Animal>\w+)'
matches_with_name = re.finditer(pattern_named_group, text)
for match in matches_with_name:name = match.group('Name')count = match.group('Count')animal = match.group('Animal')print(f"{name} has {count} {animal}")# 使用re.compile()預(yù)編譯正則表達式
pattern_compile = re.compile(r'\d+')
matches_compiled = pattern_compile.findall(text)
print("Compiled findall result:", matches_compiled) # Output: ['3', '2']
?上述示例展示了使用
re
模塊進行正則表達式的匹配、搜索、替換、分割和命名分組的功能。注釋說明了每個步驟的作用和預(yù)期輸出,通過合理使用正則表達式,可以快速實現(xiàn)對字符串的復(fù)雜處理需求。
4 正則表達式修飾符
????????在Python的正則表達式中,修飾符(也稱為標志或模式標志)是一些可選參數(shù),它們可以在編譯正則表達式時傳遞給re.compile()
函數(shù)或直接在正則表達式字符串中使用,用于改變匹配的行為。
????????以下是常用的正則表達式修飾符:
re.IGNORECASE
或re.I
: 忽略大小寫匹配。使用該修飾符后,可以在匹配時忽略大小寫的差異。
re.MULTILINE
或re.M
: 多行匹配。使用該修飾符后,^
和$
分別匹配字符串的開頭和結(jié)尾,還可以匹配字符串中每一行的開頭和結(jié)尾(每行以換行符分隔)。
re.DOTALL
或re.S
: 單行匹配。使用該修飾符后,.
將匹配包括換行符在內(nèi)的任意字符。
re.ASCII
或re.A
: 使非ASCII字符只匹配其對應(yīng)的ASCII字符。例如,\w
將只匹配ASCII字母、數(shù)字和下劃線,而不匹配非ASCII字符。
re.UNICODE
或re.U
: 使用Unicode匹配。在Python 3中,默認情況下正則表達式使用Unicode匹配。
re.VERBOSE
或re.X
: 使用“可讀性更好”的正則表達式??梢栽诒磉_式中添加注釋和空格,這樣可以使正則表達式更易讀。
在Python中,正則表達式修飾符(也稱為標志)是可選的參數(shù),用于調(diào)整正則表達式的匹配行為。修飾符可以在正則表達式模式的末尾添加,以影響模式的匹配方式。以下是常用的正則表達式修飾符:
下面通過示例來演示這些修飾符的用法:
import re# 不區(qū)分大小寫匹配
pattern1 = r'apple'
text1 = "Apple is a fruit."
match1 = re.search(pattern1, text1, re.I)
print(match1.group()) # Output: "Apple"# 多行匹配
pattern2 = r'^fruit'
text2 = "Fruit is sweet.\nFruit is healthy."
match2 = re.search(pattern2, text2, re.M)
print(match2.group()) # Output: "Fruit"# 點號匹配所有字符
pattern3 = r'apple.*orange'
text3 = "apple is a fruit.\noranges are fruits."
match3 = re.search(pattern3, text3, re.S)
print(match3.group()) # Output: "apple is a fruit.\noranges"# 忽略空白和注釋
pattern4 = r'''apple # This is a fruit\s+ # Match one or more whitespace charactersis # followed by "is"\s+ # Match one or more whitespace charactersa # followed by "a"\s+ # Match one or more whitespace charactersfruit # followed by "fruit"'''
text4 = "Apple is a fruit."
match4 = re.search(pattern4, text4, re.X)
print(match4.group()) # Output: "apple is a fruit"