[Tutor] Regex

P L la_spirou at hotmail.com
Mon May 3 23:56:54 EDT 2021


Hello,

Having difficulty on an assignment, I have to extract a part of a string, from one key word to another character.

For example;

string = "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)"

I would like to extract from ERROR to ticket, so "ERROR Tried to add information to closed ticket"

Here's what I have so far:

with open("syslog.log", "r") as file:
    for error in file:
        if 'ERROR' not in error:
            continue
        pattern = r'(?:ERROR) [\w].+'
        result = re.search(pattern, error)
        print(result)

but my results are the following:
"ERROR Tried to add information to closed ticket (>"

Tried to make the search stop at the first open bracket, but when I try [^\(]*, does not work.

Thank you for your time!


More information about the Tutor mailing list