mhinze.com archive

this is an archive of the old blog, ended 6/16/08





    05
    Mar

    list of US cities, all city names and states + regex groups

    DOWNLOAD: list of all us cities, all city names and states csv

     

    there was some discussion about a U.S. city list on a forum i was lurking in last week. so i took this census.gov list of places in the US and turned it into something more usable.

    it allowed me to use the neat groups feature in the regex Match class.

    groups let you get the text of sub-matches by using a key-value pair. here is my regex instantiation: 

    Regex r = new Regex(@"(?[A-Z]{2})[0-9]{7}(?\D+)");
     

    when I specify a group, with (?<groupKey>some regular expression) i can access the text of the sub-match of that regular expression by using the Groups property with the key..

    to produce the 2 letter state code from a line from the census text file:

    state = r.Match(line).Groups["state"].Value;
     

    i have tried many regex utilities and finally settled on Rad software's regular expression test tool. i love this utility. it does support groups and many other features. highly recommended.

     

     

    Technorati tags: , , , ,

    Leave a Reply

    You must be logged in to post a comment.

    © 2007 mhinze.com