解析字符串输入的正则表达式
本文关键字:正则表达式 输入 字符串 | 更新日期: 2023-09-27 17:53:01
我想用正则表达式把它解析成组
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 loop
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);
如果你的字符串可能有括号中的数字以外的内容,并且你只需要那些里面有数字的内容,你可以使用更具体的regex,如下所示:
string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<='()'d,'d(?='))"))
Console.WriteLine(match.Value);