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

Derek Basch dbasch at yahoo.com
Thu Jun 17 01:17:23 CEST 2004


Hello,

I have a string like:

string = "WHITE/CLARET/PINK/XL"

which I need to alter to this format:

string = "WHITE-CLARET-PINK/XL"

I developed the below functions to do so. However, something is wrong 
with my regular expression. It currently matches with the following:

/CLARET
/PINK
/XL

I thought perhaps I could exclude the match that includes the end of the 
string with someting like this:

r"([/]\w+[^$])"

but that didnt work either. Can anyone tell me how to exclude the last 
match? The one with the end of the string "/XL".

Thanks a million,
Derek Basch

------------------------------------------------
import re

string = "WHITE/CLARET/PINK/XL"

def replace_fs(thematch):
    thematch = thematch.group(1)
    thematch = "-" + thematch[1:]
    return thematch

bs_regex = re.compile(r"([/]\w+)").sub(replace_fs, str(string))
print bs_regex



More information about the Python-list mailing list