Changing entities

Kent Johnson kent37 at tds.net
Wed Jun 8 09:08:16 EDT 2005


Daniel wrote:
> Hello all
> 
> I need to build a program that check the sintax in a line:
> 
> SIZE (1 BYTE)
> 
> and change the correct number of bytes to the following format:
> 
> SIZE(1)
> 
> without the "BYTE" word. But, the number of bytes can be over 3 digits,
> and the entitie can't have spaces but the correct number of bytes in
> parentheses, like "SIZE (1024 BYTE)" to "SIZE(1024)".

 >>> import re
 >>> byter = re.compile(r'SIZE \((\d+) BYTE\)')
 >>> s = 'SIZE (1 BYTE)'
 >>> byter.sub(r'SIZE(\1)', s)
'SIZE(1)'
 >>> s = 'SIZE (1024 BYTE)'
 >>> byter.sub(r'SIZE(\1)', s)
'SIZE(1024)'

Kent



More information about the Python-list mailing list