Regular Expression - old regex module vs. re module

Jim Segrave jes at nl.demon.net
Fri Jun 30 16:08:31 EDT 2006


In article <R1fpg.6488$Bh.1886 at tornado.texas.rr.com>,
Paul McGuire <ptmcg at austin.rr._bogus_.com> wrote:
>"Jim Segrave" <jes at nl.demon.net> wrote in message
>news:12aat51q5ubf1a3 at corp.supernews.com...
>
>I can see that the OP omitted the concept of "@|||" centering, since the
>Python string interpolation forms only support right or left justified
>fields, and it seems he is trying to do some form of format->string interp
>automation.  Adding centering would require not only composing a suitable
>string interp format, but also some sort of pad() operation in the arg
>passed to the string interp operation.  I suspect this also rules out simple
>handling of the '^' operator as mentioned in the spec, and likewise for the
>trailing ellipsis if a field is not long enough for the formatted value.
>
>The '@' itself seems to be part of the field, so "@<<<<" would be a 5
>column, left-justified string.  A bare '@' seems to be a single string
>placeholder (meaningless to ask right or left justified :) ), since this is
>used in the doc's hack for including a "@" in the output.  (That is, as you
>said, the original spec provides no mechanism for escaping in a '@'
>character, it has to get hacked in as a value dropped into a single
>character field.)
>
>The Perl docs say that fields that are too long are truncated.  This does
>not happen in Python string interps for numeric values, but it can be done
>with strings (using the precision field).
>>>> print "%-10s" % string.ascii_uppercase
>ABCDEFGHIJKLMNOPQRSTUVWXYZ
>>>> print "%-10.10s" % string.ascii_uppercase
>ABCDEFGHIJ
>
>So if we were to focus on support for "@", "@>>>", "@<<<", "@###" and
>"@###.##" (with and without leading or trailing digits about the decimal)
>style format fields, this shouldn't be overly difficult, and may even meet
>the OP's requirements.  (The OP seemed to also want some support for
>something like "@##.###****" for scientific notation, again, not a
>dealbreaker.)

One would need a much clearer spec on what the OP really wants to do - note
that` Perl formats have the variable names embeeded as part of the
format string, so writing a simple Perl->Python converter isn't going
to work,

I've given him a good start for an re based solution, you've given one
for a pyparsing based one, at this point I'd hope the OP can take it
from there or can come back with more specific questions on how to
deal with some of the awfulness of the formats he's working with.




-- 
Jim Segrave           (jes at jes-2.demon.nl)




More information about the Python-list mailing list