Regular expression, "except end of string", question.

Doug Holton insert at spam.here
Wed Jun 16 22:17:11 EDT 2004


Derek Basch wrote:
>string = "WHITE/CLARET/PINK/XL"
>
>which I need to alter to this format:
>
>string = "WHITE-CLARET-PINK/XL"

You need the (?!...) operator: http://docs.python.org/lib/re-syntax.html

import re
#replace "/" with "-" if not followed by a word at the end of the string
print re.sub(r"/(?!\w+$)",r"-",s)

but like they said, join and split are better here instead of
using regular expressions.



More information about the Python-list mailing list