I have some date in Chines format, I tried pattern r'(d{1,2})月(d{1,2})日(d{0,4})时?至(d{0,2})月?(d{1,2})日(d{0,4})时?'
, it is not correct so I asked ChatGPT and I got r'(d{1,2})月(d{1,2})日(?:d{0,4}时?)?至(?:((d{1,2})月)?(d{1,2})日)(?:d{0,4}时?)?'
, I run code as below:
chineseDate1 = '5月28日至31日'
chineseDate2 = '5月28日15时至31日17时'
chineseDate3 = '5月28日15时至6月1日17时'
patternMyOriginal = r'(d{1,2})月(d{1,2})日(d{0,4})时?至(d{0,2})月?(d{1,2})日(d{0,4})时?'
patternByChatGPT = r'(d{1,2})月(d{1,2})日(?:d{0,4}时?)?至(?:((d{1,2})月)?(d{1,2})日)(?:d{0,4}时?)?'
matchesMyOriginal1 = [m.groups() for m in finditer(patternMyOriginal , chineseDate1)]
matchesMyOriginal2 = [m.groups() for m in finditer(patternMyOriginal , chineseDate2)]
matchesMyOriginal3 = [m.groups() for m in finditer(patternMyOriginal , chineseDate3)]
matchesByChatGPT1 = [m.groups() for m in finditer(patternByChatGPT , chineseDate1)]
matchesByChatGPT2 = [m.groups() for m in finditer(patternByChatGPT , chineseDate2)]
matchesByChatGPT3 = [m.groups() for m in finditer(patternByChatGPT , chineseDate3)]
print(f'{matchesMyOriginal1 = }')
print(f'{matchesMyOriginal2 = }')
print(f'{matchesMyOriginal3 = }')
print(f'{matchesByChatGPT1 = }')
print(f'{matchesByChatGPT2 = }')
print(f'{matchesByChatGPT3 = }')
I got resutls as below
matchesMyOriginal1 = [('5', '28', '', '3', '1', '')]
matchesMyOriginal2 = [('5', '28', '15', '3', '1', '17')]
matchesMyOriginal3 = [('5', '28', '15', '6', '1', '17')]
matchesByChatGPT1 = [('5', '28', None, None, '31')]
matchesByChatGPT2 = [('5', '28', None, None, '31')]
matchesByChatGPT3 = [('5', '28', '6月', '6', '1')]
that is not good matching, I want to get result as below, what can I set pattern?
matches1 = [('5', '28', '', '', '31', '')]
matches2 = [('5', '28', '15', '', '31', '17')]
matches3 = [('5', '28', '15', '6', '1', '17')]