【发布时间】:2014-07-10 07:02:12
【问题描述】:
输入 - 字符串:"TAG1xxxTAG2yyyTAG3zzzTAG1tttTAG1bbb"
预期结果:对TAG1 = {xxx,,ttt,bbb}, TAG2 = {yyy}, TAG3 = {zzz}.
我是使用正则表达式完成的,但我对使用 Regex.Replace 而不是使用返回值感到非常困惑。我想改进这段代码,如何实现呢?
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace TermsTest
{
class Program
{
static void Main(string[] args)
{
string[] tags = { "TAG1", "TAG2", "TAG3", "TAG4", "TAG5", "TAG6", "TAG7", "TAG8" };
string file = "TAG2jjfjfjndbfdjTAG1qqqqqqqTAG3uytygh fhdjdfTAG5hgjdhfghTAG6trgfmxc hdfhdTAG2jfksksdhjskTAG3kdjbjvbsjTAG2jskjdjdvjvbxjkvbjdTAG2jkxcndjcjbkjn";
string tag = "(" + string.Join("|", tags) + ")";
var dictionary = new Dictionary<string, List<string>>(tags.Length);
Regex.Replace(file, string.Format(@"({0})(.+?)(?={0}|$)", tag), match =>
{
string key = match.Groups[1].Value, value = match.Groups[3].Value;
if (dictionary.ContainsKey(key))
dictionary[key].Add(value);
else
dictionary[key] = new List<string> {value};
return "";
});
foreach (var pair in dictionary)
{
Console.Write(pair.Key + " =\t");
foreach (var entry in pair.Value)
{
Console.Write(entry + " ");
}
Console.WriteLine();
Console.WriteLine();
}
}
}
}
【问题讨论】:
标签: c# .net regex optimization refactoring