简体   繁体   English

使用C#匹配字符串中的模式

[英]Match the pattern in the string using c#

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" . 我如何只提取"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+" 我可以通过模式@"\\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. 我正在使用正则表达式,但是由于城市名称可能超过两个或三个字,因此我无法弄清楚如何提取城市名称。

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. 如果您的格式始终为[City], [State] is where I live.则此方法将起作用[City], [State] is where I live. and [City] never contains a comma. [City]绝不包含逗号。

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM