473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Returning a substring with Regex

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
Nov 16 '05 #1
2 1503
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: borges2003xx | last post by:
i have read finding sublist http://groups.google.it/group/comp.lang.python/browse_thread/thread/50b09a0aca285256/5156ada81fc9358a?hl=it#5156ada81fc9358a the problem was in a string to find if...
2
by: Philipp Sumi | last post by:
Hello Newsgroup I'm using a simple Regex expression to extract placeholders enclosed in "_" from a string as below: string pattern = @"_(\S+)_"; string test1 = "the _first_ and the _second_...
3
by: colinhumber | last post by:
How can I use regular expressions to search for a substring within another substring? For example, if I have the string: "This is * some text <button id="123">Some * text</button>" and I only...
6
by: joseph speigle | last post by:
hi, Does anybody know offhand what is the correct way to use substr to extract the domain name from a client_referer column as logged by mod_pgsqllog (httpd module), by correcting the following: ...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
13
by: Tony Girgenti | last post by:
Hello. Using VS.NET 2003 VB. If i have a string similar to the attached, how would i extract the "Truckname=" data from it in a loop and stay in the loop until the end of the string is reached...
3
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
Is it possible to validate a 2-character substring against a list of 13 2-character codes without a select statement to a table or parsing an array? Something like ... myChars in ("aa", "bb",...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.