[Python-Dev] \G (match last position) regex operator non-existant in python?
Ed Peschko
horos22 at gmail.com
Fri Oct 27 02:03:05 EDT 2017
All,
perl has a regex assertion (\G) that allows multiple-match regular
expressions to be able to use the position of the last match. Perl's
documentation puts it this way:
\G Match only at pos() (e.g. at the end-of-match position of prior m//g)
Anyways, this is exceedingly powerful for matching regularly
structured free-form records, and I was really surprised when I found
out that python did not have it. For example, if findall supported
this, it would be possible to write things like this (a quick and
dirty ifconfig parser):
pat = re.compile(r'\G(\S+)(.*?\n)(?=\S+|\Z)', re.S)
val = """
eth2 Link encap:Ethernet HWaddr xx
inet addr: xx.xx.xx.xx Bcast:xx.xx.xx.xx Mask:xx.xx.xx.xx
...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
"""
matches = re.findall(pat, val)
So - why doesn't python have this? is it something that simply was
overlooked, or is there another method of doing the same thing with
arbitrarily complex freeform records?
thanks much..
More information about the Python-Dev
mailing list