Returning a substring with Regex

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

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
 
Hi,

Unless you explain how the postal codes work in UK only a english person
can help you :)

Tip: look in google groups for this, you may find an expression, remember
that you can use both javascript & java regex almost without changes.


Cheers,
 
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.
 
With that I give you this.... use it for what it is worth. It should
perfrom better than Regex.

Thanks for that - I've already written something similar...

My boss signs my timesheets without which I don't get paid...

Mark
 
Unless you explain how the postal codes work in UK only a english person
can help you :)

With the *greatest* of respect, did you actually read my entire post?
to return everything before the first numerical character

"N11AA" returns "N"

and

"SW1A1AA" returns "SW"

How much more information do you require?
 
Hi,

Mark Rae said:
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?

There's is not enough information to validate the postcode, but if you only
interested in extracting the letters before the first digit then try :

string input = "SW1A1AA";
Match m = Regex.Match( input, "[A-Z]+" );
string area = m.Value;



HTH,
greetings
 
Mark said:
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"

(?<area>^[A-Z]+)
will capture your postcode area to a group named "area".
 
There's is not enough information to validate the postcode,

The postcode will have already been validated by QAS...
but if you only interested in extracting the letters before the first digit then try :

string input = "SW1A1AA";
Match m = Regex.Match( input, "[A-Z]+" );
string area = m.Value;

Perfect! Thanks very much.

Mark
 
Hi Mark,

Sorry for the post, I did not read it well, I did not drink coffee in the
morning yesterday :(

I did today ;)

Cheers,
 
Back
Top