Regular expressions in Python
johnvert at my-deja.com
johnvert at my-deja.com
Sun Sep 3 09:24:57 EDT 2000
Hello,
I have a few questions regarding the usage of regular expressions in
Python.
1) In Perl, I can do something like
if (/START(.+?)END/) {
use $1 here (value caught in (.+?))
}
what is the equivalent of Perl's $1, $2, ... in Python.
2) This question is not directly related to regular expressions,
but more to parsing text in Python in general:
I want to capture stuff between START and END, like in the
above regular expression, but START, the stuff in the middle,
and END are not necessarily on the same line. The only way I can
think of is to read the whole file into memory as a string, and
operate on that string, or read it with readlines() and join()
those to a string. Both of these approaches would be slow because
the file would be read in one slurp. Is there a way to handle
this `multiple line' parsing in a way that I can read the file
line by line, as in:
while 1:
line = file.readline()
# parse
Thank you very much.
-- john
print 'Just another Python programmer.'
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list