string:从句子中提取一个单词

时间:2013-06-05 07:06:09

标签: string c#-4.0

以下是我用户输入的输入样本,作为一天的任务列表

Meeting with developer 60min
Schedule a GoTo Meeting to discuss weekly sprint 45min
15min to code integration.

如何为我的计算提取60分钟,45分钟和15分钟的单词。

3 个答案:

答案 0 :(得分:5)

Regex.Match("Meeting with developer 60min", @"(\d+min)").Groups[1].ToString();

答案 1 :(得分:4)

var output = input.Split(' ', '\n', '\r').Where(i => i.Contains("min"));

编辑处理换行符

答案 2 :(得分:0)

试试吧

    string s = "Meeting with developer 60min "+
          "Schedule a GoTo Meeting to discuss weekly sprint 45min "+
          "15min to code integration.";
        foreach (Match match in Regex.Matches(s, @"(?<!\w)60\w+"))
        {
            Console.WriteLine(match.Value);
        }

        foreach (Match match in Regex.Matches(s, @"(?<!\w)15\w+"))
        {
            Console.WriteLine(match.Value);
        }

        foreach (Match match in Regex.Matches(s, @"(?<!\w)45\w+"))
        {
            Console.WriteLine(match.Value);
        }

或简短

var output = s.Split(' ').Where(i => i.Contains("min") || i.StartsWith("60") ||    i.StartsWith("15") || i.StartsWith("45"));
            foreach (var o in output)
            {
                Console.WriteLine(o);
            }