[Tutor] newbie string parsing question
Karl Pflästerer
sigurd at 12move.de
Thu Nov 13 16:52:32 EST 2003
On 13 Nov 2003, VN <- pv_nair at hotmail.com wrote:
> the re module. I am trying to parse an app server log, in real time,
> and want return the whole string that contains a value, like "Error".
> Any help would be great....
If you gave us a bit more info it would be easier to help.
I'm not sure if you really need a regexp. But anyway let's see.
********************************************************************
$ ipython
In [1]: import sre
In [2]: string = "I'm a server log entry which describes an Error"
In [3]: reg = sre.compile(".*Error.*")
In [4]: match = reg.search(string).group()
In [5]: match
Out[5]: "I'm a server log entry which describes an Error"
In [6]: match = reg.findall(string)
In [7]: match
Out[7]: ["I'm a server log entry which describes an Error"]
In [8]: "Error" in string
Out[8]: True
********************************************************************
Here you see in line 3 how an regular expression object is build (with
compile).
That object has some methods. In line 4 you see the `search()' method
which is used to to scan the string. Its return value is another object
which has a `group' method. As we have only one group in our regexp
which spans the whole string that is returned (line 5). In line 6 a
different method is used: `findall()'. Here the matches are entries in
a list.
In line 8 you see that you don't necessarily need a regexp. If you
search for a literal string you can simple say
`String in Searched_string'
If String is a part of Searched_string True is returned.
I hope that helps and if not give more information.
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list