C# 用于解析字符串输入的正则表达式

C# 用于解析字符串输入的正则表达式,c#,regex,C#,Regex,我想使用正则表达式将其解析为组 string input = @"(1,2)(3,4)"; Regex.Matches(input, @"\((\d,\d)\)"); 我得到的结果不仅是1,2和3,4,还有空间。你们能帮帮我吗 编辑: 我想分为2组1、2和3、4。你是如何联系到他们的?试试这个: 例如: MatchCollection matchs = Regex.Matches(input, @"\((\d,\d)\)"); foreach (Match m in matchs) {

我想使用正则表达式将其解析为组

string input = @"(1,2)(3,4)";
Regex.Matches(input, @"\((\d,\d)\)");
我得到的结果不仅是1,2和3,4,还有空间。你们能帮帮我吗

编辑:


我想分为2组1、2和3、4。

你是如何联系到他们的?试试这个:

例如:

MatchCollection matchs = Regex.Matches(input, @"\((\d,\d)\)");
foreach (Match m in matchs)
{
    rtb1.Text += "\n\n" + m.Captures[0].Value;
}

尝试查看以下模式:

(\((?:\d,\d)\))+
+
允许组重复并且可以出现一次或多次

string input = @"(1,2)(3,4)";
 MatchCollection inputMatch= Regex.Matches(collegeRecord.ToString(), @"(?<=\().*?(?=\))");

您也可以尝试foreach循环

 foreach (Match match in inputMatch)
{

}
我没有测试过这个代码

我的工作示例:

MatchCollection facilities = Regex.Matches(collegeRecord.ToString(), @"<td width=""38"">(.*?)image_tooltip");
            foreach (Match facility in facilities)
            {
                collegeDetailDH.InsertFacilityDetails(collegeDetailDH._CollegeID, facility.ToString().Replace("<td width=\"38\">", string.Empty).Replace("<span class=\"icon_", string.Empty).Replace("image_tooltip", string.Empty));
            }
MatchCollection facilities=Regex.Matches(collegeRecord.ToString(),@(.*)图像工具提示);
foreach(设施中的匹配设施)
{

collegeDetailDH.InsertFacilityDetails(collegeDetailDH.u CollegeID,facility.ToString().Replace(“,string.Empty”).Replace(您需要使用lookarounds

string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<=\().*?(?=\))"))
    Console.WriteLine(match.Value);
字符串输入=@“(1,2)(3,4)”;
foreach(Regex中的Match.Matches(input,@)(?请将您得到的输出也发布出来。不是为我…。您使用的输入与此不同吗?与此处的Dave相同。复制粘贴您的代码,结果为(1,2)和(3,4)。目前,根据您给出的描述,这是正确的。您需要发布更多详细信息,说明您希望看到的内容,以及哪些输入导致了问题。使用regexpal.com测试您的示例似乎效果良好,忽略了我放置的空格。同时向我们展示一些代码,这就是您如何从re中获得价值的方法gex匹配。
string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<=\().*?(?=\))"))
    Console.WriteLine(match.Value);
string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<=\()\d,\d(?=\))"))
    Console.WriteLine(match.Value);