Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将regex match.Value转换为整数? List id=ExtractIds(“英国(656)-阿伯丁(7707)”;_C#_Regex - Fatal编程技术网

C# 如何将regex match.Value转换为整数? List id=ExtractIds(“英国(656)-阿伯丁(7707)”;

C# 如何将regex match.Value转换为整数? List id=ExtractIds(“英国(656)-阿伯丁(7707)”;,c#,regex,C#,Regex,上面的列表应该用下面的方法填充,该方法从括号中去掉值 如果我使用match.Value作为字符串,并将其分配给List,它似乎可以正常工作。但当我试图将其转换为整数时,我得到了一个错误:“输入字符串的格式不正确。” 我做错了什么 List<int> ids = ExtractIds("United Kingdom (656) - Aberdeen (7707)"); 公共列表提取ID(字符串str) { MatchCollection MatchCollection=Regex.M

上面的列表应该用下面的方法填充,该方法从括号中去掉值

如果我使用match.Value作为字符串,并将其分配给List,它似乎可以正常工作。但当我试图将其转换为整数时,我得到了一个错误:“输入字符串的格式不正确。”

我做错了什么

List<int> ids = ExtractIds("United Kingdom (656) - Aberdeen (7707)");
公共列表提取ID(字符串str)
{
MatchCollection MatchCollection=Regex.Matches(str,@“\(.*?)\”);
List ExtractedIds=新列表();
foreach(匹配集合中的匹配)
{
int theid=int.Parse(match.Value);
ExtractedIds.Add(theid);
}
返回提取的EDIDS;
}

使用
match.Groups[1].Value
而不是
match.Value
只获取括号内的字符串,即不包括括号本身

使用
\d*?
而不是
?*
来确保只匹配数字,而不是括号中的任何内容

然后您甚至不再需要
,因为
\d
与右括号不匹配

您可以在正则表达式中使用lookarounds,而不是切换到look-in
组[1]

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)+”,这将分组您的号码,但忽略括号

(?<=\()\d(?=\))
公共列表提取ID(字符串str)
{
MatchCollection MatchCollection=Regex.Matches(str,@“(\d)+”);
List ExtractedIds=新列表();
foreach(匹配集合中的匹配)
{
int theid=int.Parse(match.Value);
ExtractedIds.Add(theid);
}
返回提取的EDIDS;
}

希望这能有所帮助。

在其他语言中,第0个匹配代表完整匹配字符串…对c不太肯定,但似乎是一个很好的查找位置。您正在匹配括号中的任何内容-您可能希望使用
\d+
而不是
*?
。您没有使用调试器。找出匹配的.Value是什么,您就会知道发生了什么g、 或者,您可以使用命名捕获组而不是整数索引组-请记住
match.groups[2]或更多可能也存在,以及根本没有匹配。你的代码应该考虑到这个。YUP——像这样简单的正则表达式,我不会太担心,但是任何更复杂的事情,我会考虑命名捕获。对每个人来说,非常感谢大家提供的信息和例子。烦恼。
public List<int> ExtractIds(string str)
{
     MatchCollection matchCollection = Regex.Matches(str, @"(\d)+");
     List<int> ExtractedIds = new List<int>();
     foreach (Match match in matchCollection)
     {
         int theid = int.Parse(match.Value);
         ExtractedIds.Add(theid);
      }
      return ExtractedIds;
 }