regex module, or don't work as expected
Fredrik Lundh
fredrik at pythonware.com
Tue Jul 4 11:33:03 EDT 2006
Fabian Holler wrote:
> Yes thats right, but that isn't my problem.
> The problem is in the "(?=(iface)|$)" part.
no, the problem is that you're thinking "procedural string matching from
left to right", but that's not how regular expressions work.
> I have i.e. the text:
>
> "auto lo eth0
> <MATCH START>iface lo inet loopback
> bla
> blub
>
> <MATCH END>iface eth0 inet dhcp
> hostname debian"
>
>
> My regex should match the marked text.
> But it matchs the whole text starting from iface.
which is perfectly valid, since a plain "+" is greedy, and you've asked
for "iface lo" followed by some text followed by *either* end of string
or another "iface". the rest of the string is a perfectly valid string.
if you want a non-greedy match, use "+?" instead.
however, if you just want the text between two string literals, it's
often more efficient to just split the string twice:
text = text.split("iface lo", 1)[1].split("iface", 1)[0]
</F>
More information about the Python-list
mailing list