C#正则之获取命名组所有捕获的数据
static void ParseTest(string str)
{
Regex reg = new Regex(@"(?<num>\d+)(?<sg>[_!$#])(?<dia>\d+)\s?((?<frn>\d+)(\/(?<nrn>\d+))*)?");
var m = reg.Match(str);
if (m.Success)
{
Console.WriteLine($"解析{str}成功!");
Console.WriteLine($"num:{m.Groups["num"].Value}");
Console.WriteLine($"sg:{m.Groups["sg"].Value}");
Console.WriteLine($"dia:{m.Groups["dia"].Value}");
Console.WriteLine($"frn:{m.Groups["frn"].Value}");
var captures = m.Groups["nrn"].Captures;
if (captures.Count > 0)
{
Console.WriteLine("成功捕获到nrn命名组:");
foreach (Capture cp in captures)
{
Console.WriteLine(cp.Value);
}
}
}
}
static void Main(string[] args)
{
List<string> strs = new List<string>
{
"15_25","10_25 4/3/3"
};
strs.ForEach(n => ParseTest(n));
}
或
static void ParseTestA(string str)
{
Regex reg = new Regex(@"(?<num>\d+)(?<sg>[_!$#])(?<dia>\d+)\s?((?<nrn>\d+)(\/(?<nrn>\d+))*)?");
var m = reg.Match(str);
if (m.Success)
{
Console.WriteLine($"解析{str}成功!");
Console.WriteLine($"num:{m.Groups["num"].Value}");
Console.WriteLine($"sg:{m.Groups["sg"].Value}");
Console.WriteLine($"dia:{m.Groups["dia"].Value}");
var captures = m.Groups["nrn"].Captures;
if (captures.Count > 0)
{
Console.WriteLine("成功捕获到nrn命名组:");
foreach (Capture cp in captures)
{
Console.WriteLine(cp.Value);
}
}
}
}
static void ParseTestB(string str)
{
Regex reg = new Regex(@"(?<num>\d+)(?<sg>[_!$#])(?<dia>\d+)([((]-(?<delA>\d+)[))])?(\s)?((?<frn>(\d+([((]-\d+[))])?))(\/(?<nrn>\d+([((]-\d+[))])?))*)?");
Regex regA = new Regex(@"(?<num>\d+)?([((]-(?<nnum>\d+)[))])?");
var m = reg.Match(str);
if (m.Success)
{
Console.WriteLine($"解析{str}成功!");
Console.WriteLine($"num:{m.Groups["num"].Value}");
Console.WriteLine($"sg:{m.Groups["sg"].Value}");
Console.WriteLine($"dia:{m.Groups["dia"].Value}");
Console.WriteLine($"delA:{m.Groups["delA"].Value}");
Console.WriteLine($"frn:{m.Groups["frn"]}");
var mm = regA.Match(m.Groups["frn"].Value);
if (mm.Success)
{
Console.WriteLine($"frn之num:{mm.Groups["num"].Value}");
Console.WriteLine($"frn之nnum:{mm.Groups["nnum"].Value}");
}
var captures = m.Groups["nrn"].Captures;
if (captures.Count > 0)
{
Console.WriteLine("成功捕获到nrn命名组");
foreach(Capture cp in captures)
{
Console.WriteLine(cp.Value);
var mmm = regA.Match(cp.Value);
if (mmm.Success)
{
Console.WriteLine($"nrn之num:{mmm.Groups["num"].Value}");
Console.WriteLine($"nrn之nnum:{mmm.Groups["nnum"].Value}");
}
}
}
}
}
static void Main(string[] args)
{
List<string> strs = new List<string>
{
"15_25(-10)","10_25 4(-2)/3(-3)/3"
};
strs.ForEach(n => ParseTestB(n));
}
static void ParseTestC(string str)
{
string singleInfo = $@"((?<num>\d+)(?<sg>[!_$#])(?<dia>\d+))";
string plusInfo = $@"(?<singleInfo>{singleInfo})(\+(?<singleInfo>{singleInfo}))*";
string totalInfo = $@"(?<plusInfo>{plusInfo})(\/(?<plusInfo>{plusInfo}))*";
Regex regA = new Regex(totalInfo);Regex regB = new Regex(plusInfo), regC = new Regex(singleInfo);
var m = regA.Match(str);
if (m.Success)
{
Console.WriteLine($"{str}解析成功:");
var captures = m.Groups["plusInfo"].Captures;
if (captures.Count > 0)
{
foreach(Capture cap in captures)
{
Console.WriteLine("PlusInfo:"+cap.Value);
var mm = regB.Match(cap.Value);
if (mm.Success)
{
var capturesA = mm.Groups["singleInfo"].Captures;
if (capturesA.Count > 0)
{
foreach(Capture capA in capturesA)
{
Console.WriteLine("singleInfo:"+capA.Value);
var mmm = regC.Match(capA.Value);
if (mmm.Success)
{
//Console.WriteLine($"{capA.Value}解析成功:");
Console.WriteLine($"num:{mmm.Groups["num"].Value}");
Console.WriteLine($"sg:{mmm.Groups["sg"].Value}");
Console.WriteLine($"dia:{mmm.Groups["dia"].Value}");
}
}
}
}
}
}
}
}
static void Main(string[] args)
{
List<string> strs = new List<string>
{
"3_25+2_20/2_30+3_20","3_25",
"2_10/2_20+2_12+4_12/4_30+2_12"
};
strs.ForEach(n => ParseTestC(n));
}
#####
愿你一寸一寸地攻城略地,一点一点地焕然一新
#####