Regular exp matching delimited string excepting trailing delimiters?

Bengt Richter bokr at oz.net
Fri Nov 1 17:37:55 EST 2002


On Fri, 1 Nov 2002 08:27:34 -0500, "Jeff Kowalczyk" <jtk at yahoo.com> wrote:

>> 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
>
>That's really cool and much more appropriate than a RegExp, but it doesn't seem to work in
>my Python 2.1.3 or Python 2.2.1, is there any trick to it?
>
 No trick. I guess it came with 2.2.2?

 Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> help(str.rstrip)
 Help on method_descriptor:

 rstrip(...)
     S.rstrip([sep]) -> string or unicode

     Return a copy of the string S with trailing whitespace removed.
     If sep is given and not None, remove characters in sep instead.
     If sep is unicode, S will be converted to unicode before stripping

BTW, note that it said "characters [plural] in sep" so
 >>> 'A,,C,123,D,,,,,J,,,,,,,'.rstrip(',J')
 'A,,C,123,D'

and lstrip and strip work the same, e.g.,
 >>> 'A,,C,123,D,,,,,J,,,,,,,'.strip(',JA')
 'C,123,D'

so you could clean off [\r\n\t ] also, all in one swell foop ;-)

>>>> 'yada,,,,'.rstrip(',')
>Traceback (most recent call last):
>  File "<interactive input>", line 1, in ?
>TypeError: rstrip() takes no arguments (1 given)
>
>>>> mystring.rstrip(',')
>Traceback (most recent call last):
>  File "<interactive input>", line 1, in ?
>TypeError: rstrip() takes no arguments (1 given)
>
Well, if you need to go back to a regular expression, maybe this?

 >>> import re
 >>> rx = re.compile(r'(.*[^,]),*$')
 >>> s = 'A,,C,123,D,,,,,J,,,,,,,'
 >>> rx.search(s).group(1)
 'A,,C,123,D,,,,,J'

Of course, there has to be at least one non-comma in the input or the match
object returned is None:

 >>> rx.search(',,,').group(1)
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 AttributeError: 'NoneType' object has no attribute 'group'

so you may want to test the mo before doing mo.group(1), or catch
the exception. Or add something to get a null string back:

 >>> rx = re.compile(r'(.*[^,]|[^,]?),*$')
 >>> rx.search(',,,').group(1)
 ''
 >>> rx.search('').group(1)
 ''
 >>> rx.search(s).group(1)
 'A,,C,123,D,,,,,J'

Regards,
Bengt Richter



More information about the Python-list mailing list