Begineer Question : Global string substitution with re

P

peter leonard

Hi,
This is a basic question but I can't figure out what is wron - even after
reading the documentation. I have a script that normalizes strings. One of
the steps is to convert all fractions too the tag 'fraction'. For example :

import re
line = "This is the first ratio, 170/37, and this is the second 170/37 "


def normalise(text):

#Tag fractions
fraction = r'(\s+\d+\/\d+\s+)'
regfr = re.compile(fraction)
text = regfr.sub(" |fraction| ",text)

#Remove punctuation
punc = r'\,'
regpunc = re.compile(punc)
text = regpunc.sub("",text)

return text

print line,"\n"
print normalise(line),"\n"


The output from this script is :

This is the first ratio, 170/37, and this is the second 170/37

This is the first ratio 170/37 and this is the second |fraction|


I can't understand why only one of the fractions gets substituted. The
documentation for sub states that the default argument for sub is 0 which
means replace all occurences. The output of my script should be :

This is the first ratio |fraction| and this is the second |fraction|


Any help appreciated.

Peter

_________________________________________________________________
Add MSN 8 Internet Software to your existing Internet access and enjoy
patented spam protection and more. Sign up now!
http://join.msn.com/?page=dept/byoa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top