3

Possible Duplicate:
Regular expression for parsing mailing addresses

I need to split a street address that will have one of two forms.

12B Main Road
Main Road 12B

(all parts are optional)

I need to split into street name(Main), street type(Road, already have a list of possible street types), street number (12) and the street number suffix(B).

Any advice?

Will be doing this in C#.

Community
  • 1
  • 1
Luke Narramore
  • 502
  • 2
  • 6
  • 17
  • 4
    what did you try so far? – Zak Jul 25 '12 at 14:14
  • What countries is this for? "All parts are optional": it would help if you specified the parts (just because it is obvious to you doesn't mean it is obvious). – Richard Jul 25 '12 at 14:20
  • The parts are Street Name(e.g. Main), Street Number(e.g. 12), Street Number Suffix(e.g. B) and Street Type (e.g. Road). The country this for is South Africa, there seems to be no official format. – Luke Narramore Aug 21 '12 at 06:45

2 Answers2

2

considering that I don't know all of the details(how you can expect roads to be formatted), this question is very hard to answer, but I'll do what I can.

for your 2 specific examples, you can have

@"([\d|A-Z]*) ([A-Z|a-z| ]*) ([A-Z|a-z]*)"

will match the order of your first answer


@"([A-Z|a-z| ]*) ([A-Z|a-z]*) ([\d|A-Z]*)"

will match your second answer.

example:

        Regex regex = new Regex(@"([\d|A-Z]*) ([A-Z|a-z| ]*) ([A-Z|a-z]*)");

        foreach (Group g in regex.Match("12B Main Road").Groups)
        {

            Console.WriteLine(g.Value);
        }

output from that program looks something like the following

12B Main Road
12B
Main
Road
Press any key to continue . . .

Use the Regex Cheat Sheet for information on how you can modify this.

0

you should do something like this:

Match m1 = Regex.Match(input, @"^(?<number>\d+[A-Z]?)?((?<name>[a-zA-Z]+)\s)?(?<type>Road|Drive|AndSoOn)?$")
Match m2 = Regex.Match(input, @"^(?<name>[a-zA-Z]+)?\s(?<type>Road|Drive|AndSoOn)?(?<number>\d+[A-Z]?)?$");

and then check the Success property and take actions regarding the Regex.Groups. Anyway your requirements are shallow or even point to an unsafe solution.

Diego D
  • 6,156
  • 2
  • 17
  • 30