0

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

9
  • If you need just 00:01:00 then '00:01:00' can help you out Commented Sep 16, 2022 at 13:39
  • Thanks for comment. I have to get DateTime as a string. lineItems is list itself. I just wrote it as string in question in order not to add all row Commented Sep 16, 2022 at 13:41
  • The " is not really there, it just shows up when printing to tell you it's a string Commented Sep 16, 2022 at 13:42
  • 1
    Maybe you need ideone.com/YB5ZA4? Commented Sep 16, 2022 at 13:44
  • 1
    I posted an answer with a more Python-specific solution, note the re.fullmatch does not require the pattern to contain anchors. Commented Sep 19, 2022 at 9:09

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use a positive lookbehind to make sure it's preceded by ".

matches = re.search('(?<=")(\d{2}):(\d{2}):(\d{2})', str(lineItems))

1 Comment

For some reason it did not work in my case. I have written a bit-long script,: r = re.compile(r'(\d{2}):(\d{2}):(\d{2})') matches = filter(r.fullmatch, lineItems) matches = list(matches) time = str(matches[0]) # matches = str(list(matches)) duration = time.split(":") hours= float(duration[0]) + float(float(duration[1])/60) hours = f"{hours:,.2f}" lineItems[4] =hours

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.