您可以尝试以下正则表达式:
(?<=\s)([^=]+)="([^"]+)"
这是 C# 中的代码:
var input = @"<?UMBRACO_MACRO redirectto=""/sagen.aspx"" loginpage=""/Login.aspx"" macroAlias=""BrowserValidation"" />";
var matches = Regex.Matches(input, @"(?<=\s)([^=]+)=""([^""]+)""");
foreach (Match match in matches) {
Console.Write(match.Groups[1].Value);
Console.Write(" : ");
Console.WriteLine(match.Groups[2].Value);
}
这是前面代码的一个更紧凑的版本,它自动将属性名称和值对映射到字典:
var input = @"<?UMBRACO_MACRO redirectto=""/sagen.aspx"" loginpage=""/Login.aspx"" macroAlias=""BrowserValidation"" />";
var matches = Regex.Matches(input, @"(?<=\s)([^=]+)=""([^""]+)""");
var dictionary = matches.Cast<Match>()
.Select( m => new {Key = m.Groups[1].Value, Value = m.Groups[2].Value } )
.ToDictionary(pair => pair.Key, pair => pair.Value);
Console.WriteLine(dictionary);
正则表达式 101 演示