regular expression problem

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Apr 30 19:56:16 EDT 2007


En Mon, 30 Apr 2007 19:16:58 -0300, John Davis <john.davis.85 at gmail.com>  
escribió:

> Hi all,
> I have a large logged string "str". I would like to strip down "str" so  
> that
> it contains only the lines that have "ERROR" in them. Could somebody  
> give me
> and indication of how to do this?

Forget about regular expressions! This famous quote [1] is entirely  
applicable here.
Also, str is not a good name, it hides the builtin type str. Using text as  
the variable name instead:

error_lines = [line for line in text.split("\n") if "ERROR" in line]

[1] http://regex.info/blog/2006-09-15/247

-- 
Gabriel Genellina




More information about the Python-list mailing list