0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python正規表現で文字列が時刻かどうかを判定する

Last updated at Posted at 2020-04-17

正規表現で0:00から23:59までを時刻として判定したい。
時、分ともに一桁の時と二桁の時に対応したい。

以下だと、29:59も時刻と判定してしまう。

re.match(r'([0-2]?[0-9]):([0-5]?[0-9])', '29:59')
<re.Match object; span=(0, 5), match='29:59'>

以下であれば期待通りの動作だった。

re.match(r'([0-1]?[0-9]|2[0-3]):([0-5]?[0-9])', '23:59')
<re.Match object; span=(0, 5), match='23:59'>
re.match(r'([0-1]?[0-9]|2[0-3]):([0-5]?[0-9])', '24:00')

もし他に良い方法があれば是非コメント下さい!

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?