使用Python程式進行不區分大小寫的字串替換


在本文中,我們將學習如何在Python中進行不區分大小寫的字串替換。

使用的方法

以下是完成此任務的各種方法:

  • 使用re.IGNORECASE、re.escape()、re.sub()

  • 使用re.sub()、lambda、re.escape()函式

  • 使用split()、lower()和replace()函式

  • 使用split()、list()和join()函式

方法1:使用re.IGNORECASE、re.escape()、re.sub()

re.compile()函式

正則表示式模式可以與模式物件組合,然後用於模式匹配。此函式還允許在不重寫的情況下再次搜尋模式。

語法

re.compile(pattern, repl, string)

re.sub()函式

re.sub()函式返回帶有替換值的字串,它代表子字串。當我們使用此函式時,我們可以用列表替換多個元素。

語法

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

re.escape()函式

此函式返回一個字串,其中所有非字母數字字元都被反斜槓轉義。如果您想匹配可能包含正則表示式元字元的任意字面字串,可以使用此函式。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用import關鍵字匯入re(正則表示式)模組。

  • 建立一個變數來儲存輸入字串

  • 列印輸入字串。

  • 建立另一個變數來儲存要替換的輸入替換字串。

  • 初始化要替換的子字串。

  • 使用compile()、escape()和IGNORECASE屬性忽略給定字串的所有大小寫(re.IGNORECASE用於忽略大小寫)。

  • 使用正則表示式sub()函式用替換字串替換子字串。

  • 列印不區分大小寫替換後的結果字串。

示例

以下程式使用re.IGNORECASE、re.escape()、re.sub()函式返回不區分大小寫字串替換後的字串:

# importing re(regex) module
import re

# input string
inputString = "hello TuTorialsPOint python"

# printing input string
print("Input String: ", inputString)

# input replace string to be replaced with
replaceString = "java"

# substring to be replaced
subString = "tutorialspoint"

# compilation step to escape the word for all cases

# the re.IGNORECASE is used to ignore cases
compileObj = re.compile(re.escape(subString), re.IGNORECASE)

#Substitute the substring with replacing a string using the regex sub() function
resultantStr = compileObj.sub(replaceString, inputString)

# printing resultant string after replacing
print("Resultant string after replacing: ", resultantStr)

輸出

執行上述程式後,將生成以下輸出:

Input String: hello TuTorialsPOint python
Resultant string after replacing: hello java python

方法2:使用re.sub()、lambda、re.escape()函式

lambda函式

lambda函式是一個小的匿名函式。

lambda函式可以有無限/任意數量的引數,但只有一個表示式。

語法

lambda arguments : expression

示例

以下程式使用re.sub()、lambda、re.escape()函式返回不區分大小寫字串替換後的字串:

# importing re(regex) module
import re

# input string
inputString = "hello TuTorialsPOint python"

# printing input string
print("Input String: ", inputString)

# input replace string to be replaced with
replaceString = "java"

# substring to be replaced
subString = "tutorialspoint"
resultantStr = re.sub('(?i)'+re.escape(subString), lambda k: replaceString, inputString)
print("Resultant string after replacing: ", resultantStr)

輸出

執行上述程式後,將生成以下輸出:

Input String: hello TuTorialsPOint python
Resultant string after replacing: hello java python

方法3:使用split()、lower()和replace()函式

split() - 將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格。

lower() - 將字串中所有大寫字元轉換為小寫字元。

replace()函式 - 返回字串的副本,將所有出現的舊子字串替換為另一個新的子字串。

語法

string.replace(old, new, count)

示例

以下程式使用split()、lower()和replace()函式返回不區分大小寫字串替換後的字串:

# input string
inputString = "hello TuTorialsPOint python"

# printing input string
print("Input String: ", inputString)

# input replace string to be replaced with
replaceString = "java"

# substring to be replaced
subString = "tutorialspoint"

# splitting input string into a list of words
wordsList = inputString.split()

# traversing through each word of words list
for word in wordsList:

   # checking whether the current word is equal to the given substring

   # by converting them into lowercase using the lower() function
   if(word.lower() == subString.lower()):

      # replacing current word with the input replace string
      inputString = inputString.replace(word, replaceString)
print("Resultant string after replacing: ", inputString)

輸出

執行上述程式後,將生成以下輸出:

Input String: hello TuTorialsPOint python
Resultant string after replacing: hello java python

方法4:使用split()、list()和join()函式

join() - join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。

list() 函式(將序列/可迭代物件轉換為列表)。

示例

以下程式使用split()、list()和join()函式返回不區分大小寫字串替換後的字串:

# input string
inputString = "hello TuTorialsPOint python"

# printing input string
print("Input String: ", inputString)

# input replace string to be replaced with
replaceString = "java"

# substring to be replaced
subString = "tutorialspoint"

# splitting input string into a list of words
wordsList = inputString.split()

# traversing through index and word of @@ words list
for index, word in enumerate(wordsList):

   # converting current word into lowercase and checking

   # whether it is equal to substring
   if word.lower() == subString:

      # replacing that word with the given replace string
      wordsList[index] = replaceString

# converting words of wordlist into a string
resultantStr = " ".join([word for word in wordsList])
print("Resultant string after replacing: ", resultantStr)

輸出

Input String: hello TuTorialsPOint python
Resultant string after replacing: hello java python

結論

在本文中,我們學習瞭如何使用4種不同的方法來不區分大小寫地替換給定的字串。此外,我們還學習瞭如何使用正則表示式模組的IGNORECASE屬性來忽略字串的大小寫。

更新於:2023年1月27日

4K+ 次檢視

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.