[Tutor] Substring substitution

Bernard Lebel 3dbernard at gmail.com
Thu Sep 8 22:53:42 CEST 2005


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?

Thanks
Bernard


On 9/8/05, Bernard Lebel <3dbernard at gmail.com> wrote:
> Hi Kent,
> 
> This is nice!
> 
> There is one thing though. When I run the oRe.sub() call, I get an error:
> 
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py",
> line 63, in ?
>     processPath( r'C:\temp\MT_03_03_03\allo.txt', False )
>   File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py",
> line 45, in processPath
>     else: sSubString = matchShot( sSubString )
>   File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py",
> line 11, in matchShot
>     sNewString = oRe.sub( r'\10\2', sSubString )
>   File "D:\Python24\Lib\sre.py", line 260, in filter
>     return sre_parse.expand_template(template, match)
>   File "D:\Python24\Lib\sre_parse.py", line 781, in expand_template
> raise error, "invalid group reference"
> sre_constants.error: invalid group reference
> 
> 
> 
>  This is my new match function:
> 
> 
> 
> 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
> 
> 
> 
> I have read the sub() documentation entry but I have to confess that
> made things more confusing for me...
> 
> Thanks again
> Bernard


More information about the Tutor mailing list