I want regex to select 00:01:00, however, it takes 07:59:00. So decided to search not only based on date format, but to look for " in front of date format as well.
lineItems = ["2022-08-15T07:59:00,row1,"00:01:00","2022-08-15T08:00:00"]
lineItems
is a list. I have to pick the format that is similar to 00:01:00
The script I use is below:
matches = re.search('(\d{2}):(\d{2}):(\d{2})', str(lineItems))
Could you please assist to edit the script, so it looks for "
in front of datetime, but grab only DateTime without "
.
Thanks
CodePudding user response:
You could use a positive lookbehind to make sure it's preceded by "
.
matches = re.search('(?<=")(\d{2}):(\d{2}):(\d{2})', str(lineItems))
CodePudding user response:
You can use
import re
lineItems = ["2022-08-15T07:59:00","row1","00:01:00","2022-08-15T08:00:00"]
r = re.compile(r'\d{2}:\d{2}:\d{2}')
print( list(filter(r.fullmatch, lineItems)) )
# => ['00:01:00']
See the Python demo.
Note that r.fullmatch
requires a full string match, so there is no need adding anchors to the pattern.