...
Well, copy the following text to the functions tab of properties window:
import re
def formatdate(s):
return re.sub('\d{2}(?=\d{2})', splitdate, s)
def splitdate(m):
return return m.group(0) + '/'
Then add a new text object, and type
formatdate(Date)
In the Expression box. When prompted to insert the Date variable, specify a proper default value, like
041068
You will see that it is displayed on the template as 04/10/68.
Now, how does this work? First of all, we needed to import the regular expressions module re with the corresponding import statement. Then we defined the actual formatdate function, that returns the result of a substitution operation in the original strings.
...
- The second example is applying so called name casing to a string, that is, converting a string like "ruTGeR KOPerdrAAd" into "Rutger Koperdraad". To achieve this, copy the following text to teh Functions tab of the Properties window:
import re
def namecase(s):
return re.sub('\w+', capitalizematch, s)
def capitalizematch(m):
return capitalize(m.group(0))
def capitalize(s):
if len(s) > 1:
return s[:1].upper() + s[1:].lower()
elif len(s) == 1:
return s.upper()
else:
return s
The namecase function will do the work here. It looks for groups of one or more word characters. The \w indicates a word character (a letter, a digit or an underscore), the + indicates one or more. For each match, it calls the capitalizematch function, which gets the matching text from the match and calls the capitalize function to convert the first letter to uppercase and the remaining ones to lowercase.
...