[Tutor] Substring substitution

Kent Johnson kent37 at tds.net
Fri Sep 9 00:12:32 CEST 2005


Bernard Lebel wrote:
> Ok I think I understand what is going: I'm using a 0 in the
> replacement argument, between the two groups. If I try with a letter
> or other types of characters it works fine. So how can use a digit
> here?

There is a longer syntax for \1 - \g<1> means the same thing but without the ambiguity of where it ends. So you can use r'\g<1>0\2' as your substitution string.

>>def matchShot( sSubString ):
>>
>>        # Create regular expression object
>>        oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" )
>>
>>        oMatch = oRe.search( sSubString )
>>        if oMatch != None:
>>                sNewString = oRe.sub( r'\10\2', sSubString )
>>                return sNewString
>>        else:
>>                return sSubString

You don't have to do the search, oRe.sub() won't do anything if there is no match. Also if you are doing this a lot you should pull the re.compile() out of the function (so oRe is a module variable), this is an expensive step that only has to be done once.

You hinted in your original post that you are trying to find strings where the last _\d\d has only two digits. The re you are using will also match something like 'mt_03_04_044_anim' and your matchShot() will change that to 'mt_03_04_0044_anim'. If that is not what you want you have to put some kind of a guard at the end of the re - something that won't match a digit. If you always have the _ at the end it is easy, just use r"(\d\d_\d\d\_)(\d\d_)". If you can't count on the underscore you will have to be more clever.

Kent



More information about the Tutor mailing list