62,046
社区成员
发帖
与我相关
我的任务
分享
test("default.aspx?name=bbs&id=12&class=18&bbs=abc");
test("default.aspx?name=bbs&id=12&class=ret&bbs=abc");
private string test(string strValue)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("default.html", "default.aspx");
dic.Add("about.html", "default.aspx?name=about&id=2");
dic.Add("about_(\\d+).html", "default.aspx?name=about&id=$1");
dic.Add("test_(\\d+).html", "default.aspx?name=test&id=$1");
dic.Add("news_(\\d+)_(\\d+).html", "default.aspx?name=news&id=$1&class=$2");
dic.Add("bbs_(\\d+)_(\\d+)_(\\w+).html", "default.aspx?name=bbs&id=$1&class=$2&bbs=$3");
string str;
string tmp = Regex.Replace(strValue, "(?<=\\=).+?(?=&|$)", "", RegexOptions.IgnoreCase);
MatchCollection mcValues = Regex.Matches(strValue, "(?<=\\=).+?(?=&|$)", RegexOptions.IgnoreCase);
MatchCollection mc;
MatchCollection mcKeys;
string result = "";
foreach (KeyValuePair<string, string> kvp in dic)
{
str = Regex.Replace(kvp.Value, "(?<=\\=).+?(?=&|$)", "", RegexOptions.IgnoreCase);
if (tmp.CompareTo(str) == 0)
{
int keyIndex = 0;
int keyIndexOffset = 0;
bool isOk = true;
result = kvp.Key;
mcKeys = Regex.Matches(kvp.Key, "\\(.+?\\)", RegexOptions.IgnoreCase);
mc = Regex.Matches(kvp.Value, "(?<=\\=).+?(?=&|$)", RegexOptions.IgnoreCase);
for (int i = 0; i < mc.Count; i++)
{
if (mc[i].Value.StartsWith("$") && mcKeys.Count > keyIndex)
{
result = result.Insert(mcKeys[keyIndex].Index + keyIndexOffset, mcValues[i].Value);
if (!Regex.IsMatch(mcValues[i].Value, mcKeys[keyIndex].Value, RegexOptions.IgnoreCase))
{
isOk = false;
break;
}
keyIndexOffset += mcValues[i].Value.Length;
keyIndex++;
}
else if (mcValues[i].Value.CompareTo(mc[i].Value) != 0)
{
isOk = false;
break;
}
}
if (isOk)
{
result = Regex.Replace(result, "\\(.+?\\)", "", RegexOptions.IgnoreCase);
return result;
}
}
}
return "";
}