Help required with RegEx - formatting vehicle registration numbers

I am looking for some advice and help with regards to user entry of vehicle registration numbers in the UK (This idea would also apply to the UK postal codes).

In the UK there are many different forms of vehicle registration number, always alphanumeric (capitals) and always with a single space at a predesignated position, for instance

And the following RegEx captures (most) of them. My problem is that when data is entered by a user, more often than not they don’t understand the importance of the formatting, or they are simply lazy! So for the correct registration of BD67 GTY the user may type

but we need to store the correctly formatted BD67 GTY in the database.

What is the best process to go from a form to database insert or update using RegEx?

I assume the first thing is to Trim the start and end of the string, then remove any spaces to give a pure alphanumeric string. But then how do I use the RegEx to deliver the correct format for storing in the database? What is the best process?

(?<Current>^[A-Z]{2}[0-9]{2}[A-Z]{3}$)|(?<Prefix>^[A-Z][0-9]{1,3}[A-Z]{3}$)|(?<Suffix>^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(?<DatelessLongNumberPrefix>^[0-9]{1,4}[A-Z]{1,2}$)|(?<DatelessShortNumberPrefix>^[0-9]{1,3}[A-Z]{1,3}$)|(?<DatelessLongNumberSuffix>^[A-Z]{1,2}[0-9]{1,4}$)|(?<DatelessShortNumberSufix>^[A-Z]{1,3}[0-9]{1,3}$)
Community Page
Last updated: