简体   繁体   中英

python regex for timestamp followed by characters

I am quite new to python regex (regex in general) and I am trying to parse a large string containing a time stamp. So for example, I have:

s_string="process output lean six sigma logistics 12:10:24Cel telecom giant firm"
pattern = r'''^\d{2}:\d{2}:\d{2}$''' 
re.compile(pattern).findall(str(s_string))[0]

I kind of find this strange because when I try a trivial example:

import re
s_string="43:65:24"
pattern = r'''^\d{2}:\d{2}:\d{2}\?$''' 
re.compile(pattern).findall(str(s_string))[0]

.. it outputs the right result.

So, my string basically has the time stamp and looks like:

s_string="process output lean six sigma logistics 12:10:24Cel telecom giant firm"

or,

s_string="process output lean six sigma logistics 12:10:24MKst telecom giant firm"

and I am wondering how I can just extract the time stamp without the attached characters to the seconds field.

ps: I see a few examples using datetime but I d like to do this in pure regex to improve my skills.

^ and $ anchor the start and the end of the string. When the string is 43:65:25 , the start and the end are matched correctly. But if you have any text before or after the "time", the start and the end cannot be found just before and just after the "time" match. Thus, just r'''\\d{2}:\\d{2}:\\d{2}''' should find you what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM