Saturday, October 13, 2007

Regular Expressions

"In your face regular expressions!" (shaking head)
"I don't care how many hours I spent but I defeated you"
Before our code will ruin every DTD since it would change the entity name and the value, when we only want to change the values of each entity that matches the pattern
import re

print "BEGIN"
instring = 'colorsDialog.title "asdColors">\ncolorsDialog.title "Link Colors">'
# usinf Python's raw reg exp
newstring = re.sub(r'(".*)([Cc])olor', r'\1\2olour', instring)
wrongstring = re.sub(r'([Cc])olor', r'\1olour', instring)

print instring
print newstring
print wronstring
print "END"