0

Let's say my texts are:

New York, NY is where I live.
Boston, MA is where I live.
Kentwood in the Pines, CA is where I live.

How do I extract just "New York", "Boston", "Kentwood in the Pines".

I can extract State name by pattern @"\b,\s(?"<"state">"\w\w)\s\w+\s\w+\s\w\s\w+"

I am using regular expression but I'm not able to figure out how to extract city names as city names can be more than two words or three.

3
  • then remove state name from your text :) Commented Dec 18, 2013 at 18:13
  • 4
    You could do a split on the , and just retain what occurs before it. Commented Dec 18, 2013 at 18:15
  • 1
    Those examples are extremely simple. Do you have something a little more complicated to process as well? In other words, do you have situations where there is no comma or that the city name appears somewhere other than the first full string prior to a comma? Commented Dec 18, 2013 at 18:45

2 Answers 2

2

Just substring from the beginning of the string to the first comma:

var city = input.Substring(0, input.IndexOf(','));

This will work if your format is always [City], [State] is where I live. and [City] never contains a comma.

Sign up to request clarification or add additional context in comments.

Comments

2

this is want you need ..

static void Main(string[] args)
    {
        string exp = "New York, NY is where I live. Boston, MA is where I live. Kentwood in the Pines, CA is where I live.";
        string reg = @"[\w\s]*(?=,)";
        var matches = Regex.Matches(exp, reg);
        foreach (Match m in matches)
        {
            Console.WriteLine(m.ToString());
        }

        Console.ReadLine();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.