「Python」findallメソッドにIGNORECASEを指定して大文字小文字を無視する方法

環境
PyCharm 2021.3
Python 3.9.7

書式
変数名= “正規表現式文字列"
re.findall(変数名,’対象文字列’,re.IGNORECASE)
findallメソッドの3つめの引数にre.IGNORECASEを指定しています。大文字小文字を無視します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
import re
#Tが1回以上指定正規表現
str = "T+"
print('re.IGNORECASEを指定して大文字小文字を無視')
res = re.findall(str,'34T56ttma',re.IGNORECASE)
#if文結果true false
if res:
print(res[0])
print(res[1])
else:
print(res)
# coding: utf-8 import re #Tが1回以上指定正規表現 str = "T+" print('re.IGNORECASEを指定して大文字小文字を無視') res = re.findall(str,'34T56ttma',re.IGNORECASE) #if文結果true false if res: print(res[0]) print(res[1]) else: print(res)
# coding: utf-8
import re
#Tが1回以上指定正規表現
str = "T+"

print('re.IGNORECASEを指定して大文字小文字を無視')
res = re.findall(str,'34T56ttma',re.IGNORECASE)
#if文結果true false
if res:
      print(res[0])
      print(res[1])
else:
      print(res)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
re.IGNORECASEを指定して大文字小文字を無視
T
tt
re.IGNORECASEを指定して大文字小文字を無視 T tt
re.IGNORECASEを指定して大文字小文字を無視
T
tt

 

Python

Posted by arkgame