Home > Software engineering >  Extracting time from text using REGEX giving output as NONE
Extracting time from text using REGEX giving output as NONE

Time:01-21

I've a text like this.

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'

I want to extract date & time from this string. I've worked upto.

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)

output

[]

Expected output.

['02:38:20', '21:41:56']

Note: Extracting date & times using REGEX questions have various answers all over the internet. I've tried every suggestions. But, Still I'm getting nothing as output. I guess problem with string?. Can you guys help me what's wrong I'm doing?

CodePudding user response:

regex = re.compile(r'\b\d{2}:\d{2}:\d{2}\b')
regex.findall(string)

output

['21:41:56', '02:38:20']
  •  Tags:  
  • Related