Dealing with durations defined by days, hours, minutes such as “1d 3h 2m” in Python
Question: I have to validate a string with diffrent format of duration:

    
    
    1d 8h 30m
    12h 30m
    0h 59m
    59m
    10h 0m
    10h
    1d 0h 0m
    1d 0h 59m
    1d 10h 0m
    1d
    

I am new in python and don't have any idea about `regex` in python.

## **EDIT**

There is a textbox in which user insert work duration like the JIRA Worklog.
So When user insert any of above string these are valid string. But if user
insert any invalid string then i need to show error message of invalid string
format.

    
    
    1g -- invalid
    1d 3g -- invalid
    1d 3h 30j -- invalid
    

and soon

So i just wanted to validate input string.


Answer: Here's what you need:

    
    
    ^(\d+d)?\s*((?:[01]?\d|2[0-3])h)?\s*((?:[0-5]?\d)m)?$
    

In python ([see it live](https://ideone.com/DQLuWn)):

    
    
    import re
    
    p = re.compile(ur'^(\d+d)?\s*((?:[01]?\d|2[0-3])h)?\s*((?:[0-5]?\d)m)?$', re.MULTILINE | re.IGNORECASE)
    
    time = input("Please enter your value: ")
    if re.match(p, time):
        print("\nGreat " + time + " is a valid entry.")
    else:
        print("\nOups... " + time + " is NOT a valid entry.")
    

