Mark,
I know you said that it has been specified that you use Regular Expressions
but I would argue with whoever gave me this contraint to consider the expense
of Regex.
1. For this type of operation Regex will be a bit slower then just
iteration over the string.
2. Regex is a difficult "language" to master. The code would be harder to
maintain.
With that I give you this.... use it for what it is worth. It should
perfrom better than Regex.
public string GetPostCode(string postalCode)
{
StringBuilder postCode = new StringBuilder();
foreach(char bit in postalCode)
{
if (bit > 'A' && bit < 'Z')
{
postCode.Append(bit);
}
else
{
throw new ApplicationException("Invalid Postal Code");
// or just
break;
}
return postCode.ToString();
}
Good luck convincing the higher ups.
--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"Mark Rae" wrote:
Hi,
I've been set the task of using Regex to strip off the postcode area from a
UK postcode, i.e. the bit before the first numerical character, as in:
"N11AA" returns "N"
and
"SW1A1AA" returns "SW"
I've been asked *specifically* to use Regex for this - can anyone help?
Thanks,
Mark