2

I am looking for a solution in c# to extract postal code info from address.

The postal codes of following countries

Canada,US,Germany,UK,Turkey,France,Pakistan,India,Italy.

The address can be something like these

188 pleasant street, new minas, Nova Scotia b2p 6r6, Canada.

or 109 A, block 3, DHA, Karachi 75600, Pakistan.

what I want: I want to extract any alphanumerics that is adjacent to city or country name. But having difficulty creating regular expression for it

fc123
  • 898
  • 3
  • 16
  • 40
  • 2
    It will be much easier for people to help you if you show your code so far, what the inputs are, what outputs you expect, and what's going wrong. – adv12 Jan 10 '15 at 03:46
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Jan 10 '15 at 03:46
  • This sounds like the sort of thing that would be valuable. It seems to me that someone has probably already written this, and sells it. They may even have technical support. Why would you do this on your own when someone has already done a better job of it than you ever will? – John Saunders Jan 10 '15 at 03:47
  • @salniro yes. I have idea. I want to extract any alphanumerics that is adjacent to city or country name. But having difficulty creating regular expression for it – fc123 Jan 10 '15 at 03:47
  • this post might help you http://stackoverflow.com/questions/1335293/need-help-with-regex-to-extract-zip-code-from-string – Kaushik Jan 10 '15 at 05:09

2 Answers2

-1

It's quite an open-ended task. You have to follow some specific format in there. Because what will happen if there'll be two numeric strings in the address (like a case where street is a number). So two options are possible:

  • Address is always in a specific format and you know the actual format
  • The zip is always of a given length

In both case regular expressions will lead you to the solution. - For the first example, assuming the zip code is in the given order (let's say '6r6' in your original example), you can use the following regular expression pattern: "(\S+)\, ?\w+$" - For the second case, assuming the zip code is a number of 5+ digits, which comes after the first ',', then the following pattern can be used to extract it: "(,.*)+(\d{5})". The second group will be the zip code in the match.

Here is the code you can use: public static string GetSingleMatch(string address, string pattern, int group = 0) { return new Regex(pattern, RegexOptions.IgnoreCase).Match(address).Groups[group].Value; }

The "group" optional parameter indicates the regex group which will contain the zip code.

Artak
  • 2,819
  • 20
  • 31
-2

I believe it's reasonable that you assume general rule in address which the country is the last and city or state before it, so post code can be placed between city or state and country and as you stated in the example ',' is used as separator, so it can be as following :

    private string GetPostCode(string address )
    {
        string result = string.Empty;

        string[] list = address.Split(',');
        list.Reverse();
        foreach (var item in list)
        {
            // if item contains numeric postcode 
            Regex re = new Regex(@"\d+");
            Match m = re.Match(item);
            result = m.Value;
            if (!string.IsNullOrEmpty(result))
                break;
        }

        return result;
    }

I hope it would be helpful.

Morteza Azizi
  • 479
  • 5
  • 23
  • this is not the answer. you should probably tell the regex pattern or something else. – Kaushik Jan 10 '15 at 05:11
  • @KaushikKishore : Code has been updated. but still I do not to write exact code, I just wanted to give an suggestion. thanks for comment. – Morteza Azizi Jan 10 '15 at 05:27