我想解析使用正則表達式沒有成功我自己的網絡協議通過TCP傳輸的消息。正則表達式來找到兩個特殊已知字符之間的字符串
我的命令開頭!接着COMMAND_NAME和在格式參數列表或ARGUMENT_NAME = ARGUMENT_VALUE封閉在<>
例如:
!LOGIN?<USERNAME='user'><PASSWORD='password'>;
我的代碼:
public class CommandParser
{
private Dictionary<string, string> arguments = new Dictionary<string, string>();
public CommandParser(string input)
{
Match commandMatch = Regex.Match(input, @"\!([^)]*)\&");
if (commandMatch.Success)
{
CommandName = commandMatch.Groups[1].Value;
}
// Here we call Regex.Match.
MatchCollection matches = Regex.Matches(input,"(?<!\\S)<([a-z0-9]+)=(\'[a-z0-9]+\')>(?!\\S)",
RegexOptions.IgnoreCase);
//
foreach (Match argumentMatch in matches)
{
arguments.Add(
argumentMatch.Groups[1].Value,
argumentMatch.Groups[2].Value);
}
}
public string CommandName { get; set; }
public Dictionary<string, string> Arguments
{
get { return arguments; }
}
/// <summary>
///
/// </summary>
public int ArgumentCount
{
get { return arguments.Count; }
}
}