Python multiple pattern matching from a log file
Question: I've got a log file with following format:

    
    
    2016-02-18 10:01:45.423  [a-b] [one two three] [2126]
    2016-02-18 10:01:45.623  [x-y] [one two three four] [123]
    2016-02-18 10:01:45.823  [z-w] [one two three four-five] [0]
    

I'd like to split the fields into variables so that e.g. for the first line:

Field1 = 2016-02-18

Field2 = 10:01:45.423

Field3 = a-b

Field4 = one two three

Field5 = 2126

I'm trying to figure out how to get the two first fields as I managed to get
the last 3 with the following:

    
    
    >>> import re
    >>> data = """2016-02-18 10:01:45.423  [a-b] [one two three] [2126]"""
    >>> PATTERN = re.compile(r'''\[(.*?)\]''')
    >>> print (PATTERN.split(data)[1::2])
    ['a-b', 'one two three', '2126']
    >>>
    

The content of the "Field4" may vary in length and the separator between
Field2 and Field3 is 2x white space.

How do I change the code above to capture each field?

Thanks!


Answer: It can be done without regular expression also:

    
    
    with open("your_log.log") as f:
        for x in f:
            fields = x.strip().split()
            field1, filed2, field3, field4, field5 = fields[0], fields[1], fields[2], " ".join(fields[3:-1]), fields[-1]
    

