Monday, February 9, 2009

UK Common regular expressions

I thought I would share my ideas on regular expressions. What I don’t like about the RegularExpressionValidator is that you have to repeat your regular expressions over and over again. It would be so much better if you could place them in a single place. So I thought I would write about a solution I came up with a few years ago. So the first part is the list of Regular Expressions. I like the idea of putting them in their own config file.

The section allows us to have our own class that represents regular expressions. The interesting parts of the regular expressions are

  • The & is represented as a a escaped & as it is in XML
  • We only have to define things like FirstName once and if the customer changes their mind, we change it in one place.
  • The password is minimum of 6 in length and must have an upper case, a lower case and one digit.
  • Most of the regular expressions allow for é and á and various other common accents
  • The post code has gone through lots of testing with customers and is the best I have found.
  • The phone numbers are UK
<regexSection>
<
regularExpressions>
<
regexConfig name="Password" value="^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$" />
<
regexConfig name="FirstName" value="^[a-z A-Z \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,50}$" />
<
regexConfig name="LastName" value="^[a-z A-Z \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,50}$" />
<
regexConfig name="PhoneNumber" value="^(?=.{10,13}$)0([0-9] ?){8,9}[0-9]$" />
<
regexConfig name="MobilePhoneNumber" value="^(?=.{11,13}$)07([0-9] ?){8}[0-9]$" />
<
regexConfig name="Email" value="(?i)^[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|jobs|museum|asia|edu)\b" />
<
regexConfig name="JobTitle" value="^[a-z A-Z&amp;)( \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,50}$" />
<
regexConfig name="Text" value="^[a-z A-Z \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,50}$" />
<
regexConfig name="Text100" value="^[a-z A-Z&amp;!. 0-9 \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,100}$" />
<
regexConfig name="Text1000" value="^[a-z A-Z&amp;!. 0-9 \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,1000}$" />
<
regexConfig name="AlphaNumeric" value="^[0-9a-z A-Z \xc0-\xd6 \xd8-\xf6 \xf9-\xff]+$" />
<
regexConfig name="BuildingName" value="^[0-9a-z A-Z' \xc0-\xd6 \xd8-\xf6 \xf9-\xff.,-]+[-]*[0-9a-z A-Z' \xc0-\xd6 \xd8-\xf6 \xf9-\xff.,-]*$" />
<
regexConfig name="Street" value="^[a-z A-Z' \xc0-\xd6 \xd8-\xf6 \xf9-\xff.,-]+$" />
<
regexConfig name="Address" value="^[a-z A-Z-' \xc0-\xd6 \xd8-\xf6 \xf9-\xff.]+$" />
<
regexConfig name="Postcode" value="(?i)^(((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Z][0-9][ABEHMNPRVWXY]))))\s?[0-9][ABD-HJLNP-UW-Z]{2})|(GIR\s?0AA))$" />
<
regexConfig name="POBox" value="(?i)^((?!PO Box|POBox|P.O. Box|P.O.Box|P.O Box|PO. Box).)*$" />
<
regexConfig name="AccountNumber" value="^[0-9]{8}$" />
<
regexConfig name="EmployerName" value="^[0-9a-z A-Z\xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]+$" />
<
regexConfig name="SortCode" value="^[0-9]{2}$" />
<
regexConfig name="Income" value="^(0|[1-9]{1}(,[0-9]{3}){0,2}|[1-9]{1}[0-9]{0,2}(,[0-9]{3})?|[1-9]{1}[0-9]{0,6})$" />
<
regexConfig name="Expenditure" value="^([1-9]{1}(,[0-9]{3}){0,2}|[1-9]{1}[0-9]{0,2}(,[0-9]{3})?|[1-9]{1}[0-9]{0,6})$" />
<
regexConfig name="InternetID" value="^[a-z A-Z \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{6,13}$" />
<
regexConfig name="MemorableWord" value="^[a-zA-Z0-9'-]{6,20}$" />
<
regexConfig name="PIN" value="^[0-9]{5,5}$" />
<
regexConfig name="Decimal" value="^[0-9]+$" />
<
regexConfig name="DDMMYYYY" value="^((((0?[1-9]|[12]\d|3[01])(0?[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)(0?[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])0?2((1[6-9]|[2-9]\d)?\d{2}))|(290?2((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$"/>
<
regexConfig name="SubmissionName" value="^[a-z A-Z&amp;!)(# 0-9 \xc0-\xd6 \xd8-\xf6 \xf9-\xff'-]{2,100}$" />
</
regularExpressions>
</
regexSection>

No comments: