Python – 使用正則表示式匹配包含“g”後跟一個或多個“e”的單詞的程式


Python 是資料視覺化和深度學習領域最強大的程式語言。正則表示式(也稱為正則表示式)是搜尋給定文件中任何字元或文字的有效工具。Python 語言為使用者提供了從基本計算到複雜計算的多種功能。Python 方法使用各種功能來匹配給定文字中的單詞。Python 是一種高階程式語言,也是一種用途廣泛的語言,它最受開發人員青睞,並且廣泛用於資料分析。

使用正則表示式匹配單詞的程式

為了解釋這一點,我們可以舉一個例子,例如使用一個名為 text 的變數來儲存某些值。

Text = “John is a genius at playing football games”。

輸出列印為 [genius, game]

正則表示式

  • 字串開頭的 `r` 表示它應該被視為原始輸入,因此在編譯模式時不會轉義特殊字元。

  • `\b` 字元定義單詞邊界,確保只找到以“g”開頭且前面沒有字母的完整單詞。

  • 出現在空格中的每個 g-單詞,其中可能存在多個連續的 e-字元,必須以它們後面提供的另一個邊界 \b 字元結尾;所有其他在確定單詞模式之間捕獲的重複(一個 OK- 零個包含在內)e-字元都已事先計算過,此後僅返回匹配的短語,儲存在 matches 物件 format_list 資料型別中。

方法:-

  • 方法 1 - 使用 findall() 函式

  • 方法 2 - 使用 match 函式

  • 方法 3 - 使用 finditer() 函式

方法 1:使用 findall() 函式的 Python 程式來使用正則表示式匹配單詞

句子用一組單詞初始化,可以使用 findall 方法找到以“g”開頭並後跟一個或多個“e”的單詞。

演算法

  • 步驟 1 - 匯入“re”模組以使用正則表示式值

  • 步驟 2 - 將正則表示式儲存在名為“pattern”的變數中。

  • 步驟 3 - 使用正則表示式將 findall() 函式定義為兩個引數:pattern 和 sentence。

  • 步驟 4 - 最後,列印語句。

示例

#importing the re module
import re
#the input is initialized with a word starting with g and followed by one or two e’s
sentence = "geetanjali, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#Using the findall function, to match the pattern with the sentence
matching = re.findall(pattern, sentence)
#The list is returned with the string values
print("Words which are matching from the input:",matching)

輸出

Words which are matching from the input: ['geetanjali', 'gear', 'gemma']

方法 2:使用 match() 函式的 Python 程式來使用正則表示式匹配單詞

用於查詢給定句子中所有單詞出現次數的方法是 match() 方法。

演算法

  • 步驟 1 - 匯入使用正則表示式所需的模組。

  • 步驟 2 - 函式用兩個引數定義。

  • 步驟 3 - 當沒有匹配項時,它返回空列表。

  • 步驟 4 - 列印最終列表。

示例

#importing the re module
import re
#the input is initialized with word starting with g and followed by one or two e’s
sentence = "geetanjali, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#function is defined with two parameters
def matchword(pattern, sentence):
    if not sentence:
        return []
    match = re.match(pattern, sentence)
    if match:
        return [match.group()] + matchword(pattern, sentence[match.end():])
    else:
        return matchword(pattern, sentence[1:])

matching = matchword(pattern, sentence)
#The list is returned with the string values
print("Words which are matching from the input:",matching)

輸出

Words which are matching from the input: ['geetanjali', 'gear', 'gemma']

方法 3:使用 finditer() 函式的 Python 程式來使用正則表示式匹配單詞

與 findall 方法相比,finditer() 用於列印給定句子中所有以“g”開頭並後跟一個或兩個“e”的匹配項。

演算法

  • 步驟 1 - 匯入所需的“re”模組以使用正則表示式值。

  • 步驟 2 - 初始化輸入並由各種單片語成。

  • 步驟 3 - 將正則表示式儲存在名為“pattern”的變數中。

  • 步驟 4 - 使用正則表示式將 findall() 函式定義為兩個引數:pattern 和 sentence。

  • 步驟 5 - 最後,列印語句。

示例

#importing the re module
import re
#the input is initialized with a word starting with g and followed by one or two e’s
sentence = "geek, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#Using the finditer function, to match the pattern with the sentence
matches = re.finditer(pattern, sentence)
#for loop is used to iterate through the sentence
for match in matches:
    print(match.group())

輸出

geek
gear
gemma

結論

正則表示式的一個常見用途是搜尋包含特定字元序列的單詞,例如“g”後跟一個或多個“e”。可以使用 Python 及其內建的正則表示式模組輕鬆匹配此模式。使用各種方法解釋了匹配單詞出現次數的各種方法。

更新於: 2023年9月4日

61 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.