Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting time from text using REGEX giving output as NONE

I’ve a text like this.

string = '\n# From r Wed Oct 30 21:41:56 2002\n# Return-Path: <james_ngola2002@maktoob.com>\n# X-Sieve: cmu-sieve 2.0\nReturn-Path: <james_ngola2002@maktoob.com>\n# Message-Id: <200210310241.g9V2fNm6028281@cs.CU>\n# From: "MR. JAMES NGOLA." <james_ngola2002@maktoob.com>\n# Reply-To: james_ngola2002@maktoob.com\n# To: webmaster@aclweb.org\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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[]

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?

>Solution :

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

output

['21:41:56', '02:38:20']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading