所以我试图将正则表达式结果放入列表中。
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
答案 0 :(得分:0)
事实上,这是一个警告,而不是错误。而不是
List<string> usernames = new List<string>();
写
usernames = new List<string>();
此警告显示在以下类中:
public class MyClass
{
List<string> list;
public MyClass()
{
}
}
或
public class MyClass
{
List<string> list;
public MyClass()
{
List<string> list = new List<string>();
}
}
但不是这一个:
public class MyClass
{
List<string> list;
public MyClass()
{
list = new List<string>();
}
}