Regular exp matching delimited string excepting trailing delimiters?

Bengt Richter bokr at oz.net
Thu Oct 31 21:41:50 EST 2002


On Thu, 31 Oct 2002 14:39:36 -0500, "Jeff Kowalczyk" <jtk at yahoo.com> wrote:

>"Alex Martelli" <aleax at aleax.it> wrote in message
>news:D_ew9.56722$aL4.1716505 at news1.tin.it...
>> Jeff Kowalczyk wrote:
>>
>> > Can anyone suggest a reg exp (using re) that will match the entirety of a
>> > delimited string of values, omitting zero or more delimiters at the end of
>> > the string? For example:
>> >
>> > from 'A,B,C,123,D,E,,,,,,' with delimiter ',' match 'A,B,C,123,D,E'
>> >
>> > I have (,*\Z) to match the trailing delimiters for removal with string
>> > slicing, but I'd prefer to directly match the text to keep, or match both
>> > keep and discard as groups. What would be the syntax for an omission like
>> > that? Thanks.
>>
>> Perhaps:
>>     ([^,]+,?)*
>> but that won't work if you can have e.g. A,B,,,,D,E,Z,,, ("empty fields").
>>
>Thanks, I was going to follow up that detail omitted from the original post, I *will*
>usually have internally repeating delimiters:
>'A,,C,123,D,,,,,J,,,,,,,'
>My only group to omit is zero or more trailing delimiters. Everything else must be
>verbatim.
>
You mean just trim the tail of commas? In that case just skip the re and do

 >>> 'A,,C,123,D,,,,,J,,,,,,,'.rstrip(',')
 'A,,C,123,D,,,,,J'


Regards,
Bengt Richter



More information about the Python-list mailing list