1

I have a string that shows how much time is left:

text = """                9d 15h left <br />
                           some other text not important
                           12h 5m left <br />""" 
pattern = "((\d+)d)?.*left <br />"

I'd like to match the number of days, or 9. However, if that's missing, I'd like to match an empty string. This is what I get

>>> re.findall(pattern,text)
[('', ''),('', '')]

But what I'm looking for is

>>> re.findall(pattern,text)
[('9d', '9'),('', '')]

1 Answer 1

1

you are missing the spaces in the pattern:

either:

re.search(r"[ ]+((\d+)d)?.*left <br />", text).groups()

or strip the text before

re.search(r"((\d+)d)?.*left <br />", text.strip()).groups()
Sign up to request clarification or add additional context in comments.

Comments

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.