Looking for a regular expression for this...
John Machin
sjmachin at lexicon.net
Fri Jul 28 22:10:02 EDT 2006
malahal at us.ibm.com wrote:
> OK, I tried this one. I am actually trying to parse dhcpd.conf file.
>
> def get_filename(self):
> p = "^[ \t]*filename[ \t]+(\S+).*?host[ \t]+%s\s*$" % self.host
> pat = re.compile(p, re.MULTILINE | re.DOTALL)
> mo = pat.search(self.confdata)
> if mo:
> return mo.group(1)
> else:
> return ""
>
> self.host is the hostname and self.confdata is the string. It actually
> matches the first filename that appears before the host entry. I want
> the last one that appears before the host entry. I tried '.*?' assuming
> it works, but now I know why it doesn't work!
>
> Since I am only interested in a particular host's filename, I could
> easily parse line by line. That is how it is done now, but would like to
> know if there any RE that does the trick!
>
Instead of
.*?
try
(?:.(?!filename))*?
Now forget about it and go back to the presumably legible code that you
have already :-)
Cheers,
John
More information about the Python-list
mailing list