如何查找具有特定模式的匹配字符串

时间:2015-04-17 11:40:46

标签: c# string

我需要从大字符串缓冲区中找出所有匹配特定模式的字符串。

例如,我有一个像

这样的字符串
"2014:11:12 13:30:05 Tested for ABCD at 12:30 2014:11:12 13:31:05 Tested for ABBB at 12:31 2014:11:12 13:32:05 Tested for ABCC at 12:32"

我希望得到这样的输出:

2014:11:12 13:30:05 ABCD 12:30
2014:11:12 13:31:05 ABBB 12:31
2014:11:12 13:32:05 ABCC 12:32

我尝试使用类似的代码:

string data = "2014:11:12 13:30:05 Tested for ABCD at 12:30 2014:11:12 13:31:05 Tested for ABBB at 12:31 2014:11:12 13:32:05 Tested for ABCC at 12:32";
MatchCollection matches =  Regex.Matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} Tested for [?]{4} at [0-9]{2}:[0-9]{2}");

/* But matches.count is always 0 here, looks like the pattern [?]{4} not work */

foreach (Match match in matches)
{
    string temp = match.Value;
    Match dateTime = Regex.Match(temp, "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}");

    Match Org = Regex.Match(temp, "Tested for [?]{4}").Value, "[?]{4}");

    Match processingTime = Regex.Match(temp, "at [0-9]{2}:[0-9]{2}").Value, "[0-9]{2}:[0-9]{2}");

    /* then parse the field datetime, Org and processingTime and write to console */
}

1 个答案:

答案 0 :(得分:0)

你可以使用Regex来做到这一点。我不是它的专家,我确​​信这个工作存在一个更好的正则表达式,但这应该可以解决问题(省略错误捕获)。

string input = "2014:11:12 13:30:05 Tested for ABCD at 12:30 2014:11:12 13:31:05 Tested for ABBB at 12:31 2014:11:12 13:32:05 Tested for ABCC at 12:32";
string regex = @"(\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}) Tested for (.*?) at (\d{2}:\d{2})";
Regex r = new Regex(regex);

var matchCollection = r.Matches(input);
var outputStrings = matchCollection.Cast<Match>().Select(m => string.Format("{0} {1} {2}", m.Groups[1], m.Groups[2], m.Groups[3])).ToList();
outputStrings.ForEach(str => Console.WriteLine());