LoginSignup
2
0

More than 3 years have passed since last update.

【Python3.6】電話番号の正規表現を書いてみた

Last updated at Posted at 2019-06-07

実装

Pythonで書いてみました。
「()」付きや「-」付きの場合も対応しました。


import re

pattern = r'''(
    (\d{3,4}|\(\d{3,4}\))
    (-)?
    (\d{4})
    (-)?
    (\d{4})
)'''
phone_regex = re.compile(pattern, re.VERBOSE)
text = "(090)12345678"
match = phone_regex.search(text)
if match is not None:
    print("電話番号です")
else:
    print("電話番号じゃありません")

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0