これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2024/12/21 11:44:57.596 (台灣標準時)
実際に、実行してみると、以下のようになる。#!/usr/pkg/bin/python3.9 # importing regular expression module import re # string string = 'Mon Jan 25 13:25:59 CST 2021' # making a regular expression pattern pattern = re.compile ('(\d{2}):(\d{2}):(\d{2})') # pattern matching match = re.search (pattern, string) # printing result print ("original string =", string) if (match): hour = int (match.group (1)) minute = int (match.group (2)) second = int (match.group (3)) print ("HH:MM:SS = %02d:%02d:%02d" % (hour, minute, second) )
% ./test_re_0.py original string = Mon Jan 25 13:25:59 CST 2021 HH:MM:SS = 13:25:59
実行すると以下のようになる。#!/usr/pkg/bin/python3.9 # importing regular expression module import re # string string = 'Mon Jan 25 13:25:59 CST 2021' # making a regular expression pattern pattern = re.compile ('^(\S+)\s+(\S+)\s+(\S+)\s+(\d+:\d+:\d+)\s+(\S+)\s+(\d+)$') # pattern matching match = re.search (pattern, string) # printing result print ("original string =", string) if (match): dayofweek = match.group (1) month = match.group (2) day = int (match.group (3)) hhmmss = match.group (4) timezone = match.group (5) year = int (match.group (6)) print ("%s %02d/%s/%04d (%s)" % (hhmmss, day, month, year, timezone) )
% ./test_re_1.py original string = Mon Jan 25 13:25:59 CST 2021 13:25:59 25/Jan/2021 (CST)