我正在做一個Python練習,而且我被困在這個部分,我必須使用re來檢測字符串中的日期。在python中用字符串選擇的問題
我唯一的問題是,當一天是「1st」時,它輸出一個空白字符串。我究竟做錯了什麼?
import re
text = "article 1st May 1988; another article 2 June 1992, some new article 25 October 2001; "
result = re.findall(r'(\d*) ([A-Z]\w+) (\d+)',text)
print(result)
輸出
[('', 'May', '1988'), ('2', 'June', '1992'), ('25', 'October', '2001')]
感謝您的幫助
'st'與任何東西都不匹配。請注意,'[A-Z]'與空格不匹配,'\ d *'也將匹配0個數字。 –