UK postcodes can be made up of five to seven characters and digits defined by the royal mail. The "outcode" is one or two alphabetic characters followed by one or two digts, or one or two characters followed by a digit and a character. The "incode" is a single digit followed by two characters (excluding C, I, K, M, O and V). The incode and outcode are seperated by a space. RegEx = [A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}. Below is a Coldfusion based example using this RegEx.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<!--- example text ---> <cfset text = "Postcode 1 - N5 1BU, Postcode 2 - LM41 2YB, Postcode 3 - NW1 4TH"> <!--- Attempt to find match ---> <cfset AlteredText = reReplaceNoCase(text,"[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}"," xxxx xxxx","All")> <!--- Display the results ---> <cfoutput>#AlteredText#</cfoutput>
1<!--- example text ---> <cfset text = "Postcode 1 - N5 1BU, Postcode 2 - LM41 2YB, Postcode 3 - NW1 4TH"> <!--- Attempt to find match ---> <cfset AlteredText = reReplaceNoCase(text,"[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}"," xxxx xxxx","All")> <!--- Display the results ---> <cfoutput>#AlteredText#</cfoutput>
AlteredText = Postcode 1 - xxxx xxxx, Postcode 2 - xxxx xxxx, Postcode 3 - xxxx xxxx