[Tutor] Regex
Peter Otten
__peter__ at web.de
Tue May 4 09:59:04 EDT 2021
On 04/05/2021 15:15, P L wrote:
> The assignment is to search for the word ERROR and the sentence that follows it. There is no terminator to the sentence, simply a blank space, as the following:
>
> Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)
Well, if the sentence is guaranteed to be followed by a space and an
opening parenthesis you can use that as the "terminator":
>>> s = "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add
information to closed ticket (mcintosh)"
>>> import re
>>> re.search(r"(ERROR .*?) \(", s).group(1)
'ERROR Tried to add information to closed ticket'
Alternatively, with a lookahead assertion:
>>> re.search(r"ERROR .*?(?= \()", s).group()
'ERROR Tried to add information to closed ticket'
More information about the Tutor
mailing list