[CentralOH] Regular Expression and String Format Expression

Mark Erbaugh microenh at hughes.net
Mon Oct 18 01:03:37 CEST 2010


I'm working on some code allows the user to configure certain input and output formats. For example, an account number consists of up to three integers (L1, L2 and L3). Using a regular expression, the user can configure how those three values are entered, i.e. 11-22-3333 or 3333.2211 or whatever they want.

Currently, I'm requiring the user to enter another string which is applied to Python's % (string format) operator to determine how to display an account number. The user is required to enter matching regular expression and string formats.

For example, if the format is 11-22-3333, the user could enter 

a regular expression of r'(?P<L1>\d\d)-(?P<L2>\d\d)-(?P<L3>\d\d\d\d)$'
and a format string of '%(L1)02d-%(L2)02d-%(L3)04d'

Is there a way to automatically create the string format expression from the regular expression or vice versa? 

Mark




More information about the CentralOH mailing list