0
0

More than 1 year has passed since last update.

python 正規表現 忌々しい日付のゼロを取り除く

Last updated at Posted at 2022-02-12

月と日のあたまにあるゼロをとって、ついでに、日付を世界標準形式のハイフンに変換します。

import re

text = '2022年01月01日'

pattern = r'([12]\d{3})[\.\-年](0?)([1-9]|1[0-2])[\.\-月](0?)([1-9]|[1-2][0-9]|3[0-1])日?$'
if re.match(pattern, text):
    print(re.sub(pattern, r'\1-\3-\5', text))
else:
    print('no match')
'2022-1-1'
0
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
0
0