繁体   English   中英

正则表达式解析字符串输入

[英]Regex to parse string input

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

string input = @"(1,2)(3,4)";
Regex.Matches(input, @"\((\d,\d)\)");

我得到的结果不仅是1,2和3,4,而且还有空格。 你们能帮我吗?

编辑:

我想得到2组1,2和3,4。

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

对于当前字符串,您将获得两个输出:

inputMatch[0].Groups[0].Value;
inputMatch[0].Groups[1].Value;

要么

您也可以尝试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 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)";
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM