我有這樣的文字。
string = '\n# From r Wed Oct 30 21:41:56 2002\n# Return-Path: <[email protected]>\n# X-Sieve: cmu-sieve 2.0\nReturn-Path: <[email protected]>\n# Message-Id: <[email protected]>\n# From: "MR. JAMES NGOLA." <[email protected]>\n# Reply-To: [email protected]\n# To: [email protected]\n# Date: Thu, 31 Oct 2002 02:38:20 0000'
我想從這個字串中提取日期和時間。我已經盡力了。
import re
regex = re.compile(r'\b\d{4}[-/]\d{2}[-/]\d{2}\s\d{2}:\d{2}:\d{2}\s[- ]\d{4}\b')
regex.findall(string)
輸出
[]
預期輸出。
['02:38:20', '21:41:56']
注意: 使用 REGEX 問題提取日期和時間在互聯網上有各種答案。我已經嘗試了每一個建議。但是,我仍然沒有得到任何輸出。我猜字串有問題?你們能幫我看看我在做什么錯嗎?
uj5u.com熱心網友回復:
regex = re.compile(r'\b\d{2}:\d{2}:\d{2}\b')
regex.findall(string)
輸出
['21:41:56', '02:38:20']
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418906.html
標籤:
上一篇:Wordle的正則運算式
