Python regular expression, remove repeated class
Question: I have a bunch of timestamp strings. Each string has three sections and
delimiters between sections. A delimiter may be any of these [.:,;] symbols.
Each section contains digits. The first one may contain one or two digits. Any
other section may contain two digits. I need to retrieve those digits and
fulfil some actions with them. I use Python3.

So I write this code:

    
    
    import re
    lines = ('1:24.15', '17.01.01', '05:07:28', '175.11.123', '4:35,07', '01;21;73', '00;1;1', '7;7.12')
    pattern = re.compile(r'^(\d{1,2})[:.,;](\d{2})[:.,;](\d{2})$')
    for i in lines:
        try:
            mm, ss, ff = pattern.search(i).groups()
        except AttributeError:
            print('{} is invalid'.format(i))
        print(int(mm) * 60 + int(ss) + round(int(ff) / 0.075 / 1000, 3))
    

My question is... How could I reduce the repetitions in this regular
expression?

    
    
    r'^(\d{1,2})[:.,;](\d{2})[:.,;](\d{2})$'
    

Thank you in advance. I'll be thankful for any advice.


Answer: Why don't use
[`re.split()`](https://docs.python.org/2/library/re.html#re.split):

    
    
    pattern = re.compile(r"[.:;,]")
    for line in lines:
        mm, ss, ff = pattern.split(line)
    

Though, this would require an additional length check of `mm`, `ss` and `ff`.
On one hand - this makes things less attractive, but this leads to _more
precise and meaningful error messages_ :

    
    
    pattern = re.compile(r"[.:;,]")
    for line in lines:
        try:
            mm, ss, ff = pattern.split(line)
        except ValueError:
            print('{} has not enough digit groups'.format(line))
            continue
    
        if len(mm) not in (1, 2) or len(ss) != 2 or len(ff) != 2:
            print('{} has a digit group with invalid length'.format(line))
            continue
    
        print(int(mm) * 60 + int(ss) + round(int(ff) / 0.075 / 1000, 3))
    

