Python re モジュール
re モジュールを使うと、正規表現でテキストの検索、照合、分割、置換ができます。
import re
正規表現は、テキストのパターンを表します。r'\d+' のような raw string を使うと、バックスラッシュが正しく正規表現エンジンに渡されます。
search()
search は、文字列のどこかにある最初の一致を見つけます。
import re
match = re.search(r'\d+', 'Order #12345 shipped')
print(match.group())
12345
findall()
findall は、重なりのない一致をすべて返します。
import re
emails = re.findall(r'[\w.-]+@[\w.-]+', 'a@example.com b@example.com')
print(emails)
['a@example.com', 'b@example.com']
sub()
sub は一致したテキストを置換します。
import re
message = re.sub(r'\s+', ' ', 'too many spaces')
print(message)
too many spaces
パターンをコンパイルする
同じ式を繰り返し使う場合、コンパイル済みパターンが便利です。
import re
pattern = re.compile(r'^python', re.IGNORECASE)
print(bool(pattern.match('Python Cheatsheet')))
True
キャプチャグループ
丸括弧は一致した部分の一部をキャプチャします。
import re
match = re.search(r'(\w+)=(\d+)', 'count=42')
print(match.group(1))
print(match.group(2))
count
42
一致しない場合の処理
group() を呼ぶ前に、まず一致があるかどうかを確認してください。
import re
match = re.search(r'\d+', 'no number here')
if match:
print(match.group())
else:
print('No match')
No match