Anyone that has a CardExchange® Producer Business Edition license can use the Python script language that is integrated in CardExchange® . One of the powerful features of Python is the support of regular expressions. Extensive documentation on regular expressions can be found here . Below, I will give to examples of the way regular expressions can be used to manipulate strings.
- Imagine, your database has a text field that contains six digits to represent a date, for example '041068', which I want to display on the card template as '04/10/68'. How can I achieve this with regular expressions?
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 first, and certainly most complicated, argument of the re.sub function is the search pattern. This patters searches for pairs of two digits \d{2} that are followed by another pair of two digits (?=\d{2}). The construct (?=...) is used to indicate that we require two more digits to follow the two digits we are looking for, but that these are not forming part of our match.
For each match, the function splitdate is called with the match as argument. This function gets the two digits from the match and adds the slash. There will be only two matches, as the third pair of digits (68) is not followed by two more digits. It is dense and complicated programming, but you can see that you can achieve the result with just a few lines of code.
- 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.
The capitalize functions needs to check the length of the text to avoid error messages if a one character word is found. s[:1] gets the first character of the word, whereas s[1:] gets all characters that follow the first one.