List<int> ids = ExtractIds("United Kingdom (656) - Aberdeen (7707)");
上面的列表应通过以下方法填充,该方法从括号内提取值。
如果我将match.Value作为字符串并将其分配给List< string >,似乎可以正常工作。 但是当我尝试将其转换为整数时,出现错误:“输入字符串格式不正确”。
我做错了什么?
public List<int> ExtractIds(string str)
{
MatchCollection matchCollection = Regex.Matches(str, @"\((.*?)\)");
List<int> ExtractedIds = new List<int>();
foreach (Match match in matchCollection)
{
int theid = int.Parse(match.Value);
ExtractedIds.Add(theid);
}
return ExtractedIds;
}
\d+
而不是.*?
。 - cfeduke