Pythonのif文を利用して”正規表現”で条件分岐。電話番号、メールアドレスなどよく使うもパターンも紹介

Pythonのif文で正規表現を利用して条件分岐をする例を紹介します。

Pythonではreモジュールを使います。

※ reモジュールは標準ライブラリなので、特別にインストールする必要はありません。
参考:正規表現操作

その他チートシート的にまとめてある記事もありますので参考にしてください。

あわせて読みたい
Pythonの正規表現でよく使うパターンのテンプレート Pythonの正規表現でよく使うパターンをまとめました。 以下が今回の記事でマッチさせる正規表現のパターンになります。 もちろんPythonの標準ライブラリreオブジェクト...
目次

if文で正規表現を利用

シンプルな基本形は以下になります。

#正規表現パターンをコンパイル: compile()
if re.compile(正規表現パターン).search(文字列):
    #マッチした場合
    print(Match)
else:
    #マッチしなかった場合
    print(Not match)

以下が実行結果になります。

import re

match_one = hello world
match_two = hi world

pattern = hello

if re.compile(pattern).search(match_one):
    print(match_one+:+Match)
else:
    print(match_one+:+Not Match)

# hello world:Match

if re.compile(pattern).search(match_two):
    print(match_two+:+Match)
else:
    print(match_two+:+Not Match)

# hi world:Not Match

数値やアルファベット

i数値やアルファベットのマッチパターンのif文は以下です。

if re.compile([a-zA-Z0-9]).search(文字列):

サンプルは以下になります。

import re

has_num = '東京1'
has_lower = '東京a'
has_upper = '東京A'
has_nothing = '東京'

pattern = r[a-zA-Z0-9]

if re.compile(pattern).search(has_num):
    print(has_num+:+Match)
else:
    print(has_num+:+Not Match)

# 東京1:Match

if re.compile(pattern).search(has_lower):
    print(has_lower+:+Match)
else:
    print(has_lower+:+Not Match)

# 東京a:Match        

if re.compile(pattern).search(has_upper):
    print(has_upper+:+Match)
else:
    print(has_upper+:+Not Match)

# 東京A:Match        

if re.compile(pattern).search(has_nothing):
    print(has_nothing+:+Match)
else:
    print(has_nothing+:+Not Match)

# 東京:Not Match

アルファベットの大文字小文字を無視してマッチ

アルファベットで大文字と小文字を同一なものとしてマッチさせるのは以下になります。

if re.compile([a-zA-Z]).search(文字列):

サンプルは以下になります。

import re

lowercase = hello
uppercase = HELLO

pattern = [a-zA-Z]

if re.compile(pattern).search(lowercase):
    print(lowercase+:+Match)
else:
    print(lowercase+:+Not Match)

# hello:Match

if re.compile(pattern).search(uppercase):
    print(uppercase+:+Match)
else:
    print(uppercase+:+Not Match)

# HELLO:Match

電話番号の形式

様々な電話番号パターンを用意しました。

# 一般的な電話番号
if re.compile([0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}).search(文字列):
# 携帯電話
if re.compile(0[789]0-[0-9]{4}-[0-9]{4}).search(文字列):
# 03で始まる
if re.compile(03-[0-9]{2,4}-[0-9]{3,4}).search(文字列):

サンプルは以下になります。

import re

common = 0120-123-123
cell = 080-1111-2222
one_three = 03-1111-2222

if re.compile([0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}).search(common):
    print(common+:+Match)
else:
    print(common+:+Not Match)

# 0120-123-123:Match

if re.compile([0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}).search(cell):
    print(cell+:+Match)
else:
    print(cell+:+Not Match)

# 080-1111-2222:Match

if re.compile([0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}).search(one_three):
    print(one_three+:+Match)
else:
    print(one_three+:+Not Match)

# 03-1111-2222:Match

7桁の郵便番号

日本の一般的な郵便番号7桁の場合は以下のように実施します。

if re.compile(\b[0-9]{3}-[0-9]{4}\b).search(文字列):

サンプルは以下になります。

import re

pc_one = My Postal code is 111-2222
pc_two = My Postal code is 1111-2222

pattern = r\b[0-9]{3}-[0-9]{4}\b

if re.compile(pattern).search(pc_one):
    print(pc_one+:+Match)
else:
    print(pc_one+:+Not Match)

# My Postal code is 111-2222:Match

if re.compile(pattern).search(pc_two):
    print(pc_two+:+Match)
else:
    print(pc_two+:+Not Match)

# My Postal code is 1111-2222:Not Match

co.jpや.comなどメールアドレス

ico.jpや.comなどメールアドレスも正規表現でマッチ可能です。

if re.compile([a-zA-Z0-9_.+-]+@(?:[a-zA-Z0-9]+\.){1,}[a-zA-Z0-9]+).search(文字列):

サンプルは以下になります。

import re

email_one = My email is python@yahoo.co.jp
email_two = My email is python@gmail.com
email_three = My email is Nothing

pattern = [a-zA-Z0-9_.+-]+@(?:[a-zA-Z0-9]+\.){1,}[a-zA-Z0-9]+

if re.compile(pattern).search(email_one):
    print(email_one+:+Match)
else:
    print(email_one+:+Not Match)

# My email is python@yahoo.co.jp:Match

if re.compile(pattern).search(email_two):
    print(email_two+:+Match)
else:
    print(email_two+:+Not Match)

# My email is python@gmail.com:Match

if re.compile(pattern).search(email_three):
    print(email_three+:+Match)
else:
    print(email_three+:+Not Match)

# My email is Nothing:Not Match

co.jpや.comなどドメイン

co.jpや.comなどドメインのマッチも可能です。

if re.compile(([a-zA-Z0-9][a-zA-Z0-9\-]{1,61}[a-zA-Z0-9]\.)+[a-zA-Z]+).search(文字列):

サンプルは以下になります。

import re

domain_one = My domain is yahoo.co.jp
domain_two = My domain is google.com
domain_three = My domain is Nothing

pattern = r([a-zA-Z0-9][a-zA-Z0-9\-]{1,61}[a-zA-Z0-9]\.)+[a-zA-Z]+

if re.compile(pattern).search(domain_one):
    print(domain_one+:+Match)
else:
    print(domain_one+:+Not Match)

# My domain is yahoo.co.jp:Match

if re.compile(pattern).search(domain_two):
    print(domain_two+:+Match)
else:
    print(domain_two+:+Not Match)

# My domain is google.com:Match

if re.compile(pattern).search(domain_three):
    print(domain_three+:+Match)
else:
    print(domain_three+:+Not Match)

# My domain is Nothing:Not Match
退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング
よかったらシェアしてね!
  • URLをコピーしました!
目次