[Python] Regex - python reading strings from a list index error

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am testing a regex pattern. I not getting matches, getting an index error and not sure why. My thought is because there is no match. I'm weak with regex and not entirely sure how it should be constructed.

    My ultimate goal is to read a text file into a list. Loop through the list and separate the time_stamps and messages out

    regex pattern

    r'(\d+/\d+/\d+ \d\d:\d\d:\d\d[.]\d\d\d\d)\n(.*)'


    Test code

    import re



    lines = ['5/22/2025 12:30:00.1234',
    '1:13.0 [test_regex]="this-is-a-test-of-regex:12345|test_of_regex|test-of-regex"']

    for line in lines:
    try:
    time_stamp, messages = re.findall(r'(\d+/\d+/\d+ \d\d:\d\d:\d\d[.]\d\d\d\d)\n(.*)', line, re.DOTALL)[0]
    except IndexError as e:
    print(f'No match found {e}')
    else:
    print (f'time_stamp {time_stamp}, message {messages}')

    Continue reading...

Compartilhe esta Página