使用正則表示式在 Python 中匹配空白字元
正則表示式,通常稱為 RegEx,是一串字元,對應於文字字串中的字母、單詞或字元模式。這意味著您可以使用正則表示式來匹配和檢索文字中的任何字串模式。搜尋和替換過程受益於正則表示式的使用。最常見的應用是搜尋與模式匹配的子字串並替換其他內容。
什麼是空白字元?
“空白字元”是指任何表示水平或垂直空間的字母或字元集。使用正則表示式,元字元“\s”在 Python 中匹配空白字元。
演算法
匯入 re 函式
初始化一個字串。
使用元字元 \s 在 Python 中匹配空白字元。
使用 findall 方法,' \s’ 元字元和字串作為引數。
列印結果並獲取匹配的空白字元。
語法
result = re.findall(r'[\s]', str) re.findall(): Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. regx = re.compile('\W') re.compile(): We can compile a regular expression into a regex object to look for occurrences of the same pattern inside various target strings without rewriting it. result = regx.findall(str) The re module provides a series of methods that let us look for matches in a string: findall: returns a list of all matches. split: Returns a list with the string split at each match. sub: substitutes a string for one or more matches.
search - 如果字串在某處有匹配項,則返回一個 Match 物件。
示例 1:如何在 Python 中匹配空白字元
#importing re function import re #initialising a string str str= 'The Psychology of Money.' #storing the value of findall method in a variable result result = re.findall(r'[\s]', str) #printing the result print('The give string is \n',str) print('It has',len(result),'WhiteSpaces') print (result)
輸出
上面程式碼中的字串有 3 個空白字元。同樣,以下是上述命令的輸出:
('The give string is \n', 'The Psychology of Money.') ('It has', 3, 'WhiteSpaces') [' ', ' ', ' ']
程式碼解釋
我們匯入 re 模組來開始使用正則表示式在 Python 中匹配空白字元。下一步是用要匹配空白字元的字串初始化變數“str”。元字元“\s”用於使用 Python 中的 RegEx 檢查空白字元。
定義為“result”的變數儲存 Python 函式 findall() 的結果。此函式搜尋整個文字中模式存在的所有例項。它採用兩個引數,元字元“[\s]”和字串“str”。最後一步是列印結果作為輸出。
示例
#importing re function import re #initializing a string str str= "Honesty is the best policy." #storing the value of findall method in a variable result result = re.findall(r'[\s]', str) #printing the result print('The given string is \n',str) print('It has',len(result),'WhiteSpaces') print (result)
輸出
上面程式碼中的字串有 4 個空白字元。同樣,以下是上述命令的輸出:
('The given string is \n', 'Honesty is the best policy.') ('It has', 4, 'WhiteSpaces') [' ', ' ', ' ', ' ']
示例
#importing re function import re #Taking input from the user and storing it in a string str str= 'Honesty is the best policy' #initialising regex, which will compile all matching word characters regx = re.compile('\W') #storing the value of findall method in a variable result result = regx.findall(str) #printing the result print('The given string is \n',str) print('It has',len(result),'WhiteSpaces') print (result)
輸出
上述命令的輸出如下:
('The given string is \n', 'Honesty is the best policy') ('It has', 4, 'WhiteSpaces') [' ', ' ', ' ', ' ']
程式碼解釋
我們載入 re 模組以開始在 Python 中使用正則表示式匹配空白字元。下一步是要求使用者輸入一個包含我們想要匹配的空白字元的字串。在 Python 中使用 RegEx 時,元字元“\s”用於匹配空白字元。
Python 方法 findall 儲存在一個名為“result”的變數中()。此方法查詢文字中模式的每個例項。它需要元字元“[\s]”和字串“str”作為兩個引數。輸出返回使用者提供的字串中存在的空白字元。
結論
正則表示式是提供搜尋模式的專用文字字串。它們是一系列字元,表示文字字串中的某些字母、單詞或字元組合。re 模組用於處理正則表示式。元字元“\s”用於使用正則表示式在 Python 中匹配空白字元。
RegEx 中最常用的函式是 findall()、search()、split() 和 sub()。錨點、字元集和修飾符是正則表示式結構的關鍵組成部分。