check Zipcode in a string

Posted on 2011-10-26
Last Modified: 2012-08-13
How can I check if a string contain zipcode in Regex?
san francisco, ca 91000  or san francisco ca 91000

I want to make it san francisco, CA 91000

String ZipRegex = @"^\d{5}$";

  String ZipRegex = @"^\d{5}$";
   if(Regex.IsMatch(ZipTextBox.Text, ZipRegex))
      ResultLabel.Text = "ZIP is valid!";
      ResultLabel.Text = "ZIP is invalid!";

Question by:VBdotnet2005
    1 Comment
    LVL 74

    Accepted Solution

    Try this:

    String ZipRegex = @"\b\d{5}\b";

    Open in new window

    Be advised:  this will check that a string of 5 digits occurs anywhere in the string, but not that it is necessarily a zip code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now