0
所以我试图将正则表达式结果放入列表中。尝试在字符串列表中添加项目错误
List<string> usernames = new List<string>();
是我想要的列表。
Regex regex = new Regex(@"RESPONSIVE\.constant\.user = {(?<userParams>.*?)}", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match match = regex.Match(text);
if (match.Success)
{
// Split up the values using comma
var keyValuePairs = match.Groups["userParams"].Value.Split(',');
// Split up each line using : as delimeter and clean up both sides, removing whitespace and single quote characters
var dict = keyValuePairs
.Select(kvp => kvp.Split(':'))
.ToDictionary(kvp => kvp[0].Trim(), kvp => kvp[1].Trim().Trim(new char[] { '\'' }));
// Read name
var name = dict["name"];
usernames.Add(name);
}
是什么,我试图做的,但我得到了以下错误:
Warning CS0649 Field 'Form1.usernames' is never assigned to, and will always have its default value null
错误消息非常清晰 - 您需要在Form1中实例化(创建)'usernames'实例。简单地宣称这是不够的。 – Tim
你确定你添加的'用户名'与你在问题中的顶部相同吗? – CodingYoshi
@Tim OP已经实例化了它。我想 - 如果它是同一个。 – CodingYoshi