ウェブサイト検索

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(regex) モジュールをインポートします。

  • 入力文字列を保存する変数を作成します。

  • 入力文字列を出力します。

  • 置換される入力置換文字列を保存する別の変数を作成します。

  • 置換する部分文字列を初期化します。

  • 指定された文字列の大文字と小文字をすべて無視するには、compile()、escape()、および IGNORECASE 属性を使用します (大文字と小文字を無視するには、re.IGNORECASE を使用します)。

  • regex 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() 関数を使用する

ラムダ関数

ラムダ関数は小さな匿名関数です。

ラムダ関数には無制限または任意の数の引数を指定できますが、式は 1 つだけです。

構文

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 属性を使用して文字列の大文字と小文字を無視する方法を学びました。

関連記事: