我做了一個小的解析器,它會通過一個消息並用特定的用戶替換$ user,等等。小消息解析器,最新不好?
除了基本的關鍵詞,我想它來代替日期如下: $ datemo10與今天的日期加10個月 $ datedd03與今天的日期加3天 $ datehh07與今天的日期加上7個小時 $ datemm12與今天日期加上12分鐘 $ datess15與今天的日期加15秒
這是我得到的工作..
const string Identifier = "$";
const string TimeFormat = "HH:mm:ss dd-MM-yyyy";
public static string Encode(string Author, string Recipent, string input)
{
Dictionary<string, string> keywords = new Dictionary<string, string>();
keywords.Add("bye", "With kind regards " + identify("me"));
keywords.Add("user", Recipent);
keywords.Add("me", Author);
keywords.Add("now", DateTime.Now.ToString(TimeFormat));
keywords.Add("date", "");
string result = input;
foreach (KeyValuePair<string, string> keyword in keywords)
{
if (keyword.Key.ToLower() == "date")
{
int addedLength = 0;
foreach (Match match in Regex.Matches(input, "\\" + identify(keyword.Key)))
{
string mode = input.Substring(match.Index + addedLength + match.Length, 2);
string stringInteger = input.Substring(match.Index + addedLength + match.Length + 2, 2);
int integer;
if (int.TryParse(stringInteger, out integer) && !mode.Contains(" "))
{
if (mode == "ss")
{
string dateTime = DateTime.Now.AddSeconds(Convert.ToDouble(integer)).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
else if (mode == "mm")
{
string dateTime = DateTime.Now.AddMinutes(Convert.ToDouble(integer)).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
else if (mode == "hh")
{
string dateTime = DateTime.Now.AddHours(Convert.ToDouble(integer)).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
else if (mode == "dd")
{
string dateTime = DateTime.Now.AddDays(Convert.ToDouble(integer)).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
else if (mode == "mo")
{
string dateTime = DateTime.Now.AddMonths(integer).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
else if (mode == "yy")
{
string dateTime = DateTime.Now.AddYears(integer).ToString(TimeFormat);
input = input.Remove(match.Index + addedLength, match.Length + 4);
input = input.Insert(match.Index + addedLength, dateTime);
addedLength += (dateTime.Length - (match.Length + 4));
}
}
}
}
else
{
input = Regex.Replace(input, "\\" + identify(keyword.Key), keyword.Value);
}
}
return input;
}
protected static string identify(string val)
{
return Identifier + val;
}
我感覺很好關於保持需要的關鍵字在dictiona被替換ry,但我真的不喜歡解析和替換日期的方式。
但隨着即時通訊相當新的整體規劃的世界裏,這是唯一的辦法,我能夠使它發揮作用。雖然我完全能夠明白爲什麼它可以變得更好,但如果你對如何使它工作在一種不太古怪的方式有任何想法,請告訴我:)
撕開它,但請在建設性的問題。 我怎樣才能讓它變得更好?
我知道有相當多的重複的代碼..噓
謝謝:)