110,534
社区成员
发帖
与我相关
我的任务
分享
static void Main()
{
string p1 = @"(?<head>168\s129(?:\s\d+){15}\s1\s0\s\d+)", p2 = @"(?<data>\b2(?:\s\d+){2}\s48(?:\s\d+)+?\s3\s\d+)", p3 = string.Format(@"(?<block>{0}(?:\s{1})+)", p1, p2);
Regex r1 = new Regex(p1), r2 = new Regex(p2), r3 = new Regex(p3);
string str = "168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 56 52 53 49 3 77 2 49 69 48 48 48 48 48 65 66 54 53 3 69 168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 65 66 54 53 3 69";
foreach( Match m in r3.Matches(str)){
string h = m.Groups["head"].Value ; //获取数据块头部
Console.WriteLine("==Head: " + h);
if (true) //这里假设头部验证通过
{
foreach( Capture c in m.Groups ["data"].Captures ){
Console.WriteLine(" +Data: "+c.Value);
}
}
}
}
//如果里层数据和外层数据都是固定那样的话这样更简单
Regex reg = new Regex(@"((168(?:(?!\b2\b).)*)(2(?:(?!\b3\b).)*3\s\d+\s?)*)*");
foreach (Match m in reg.Matches(str))
{
foreach (Capture c in m.Groups[2].Captures)
Console.WriteLine(c.Value);
foreach (Capture c in m.Groups[3].Captures)
Console.WriteLine(c.Value);
}
Console.ReadLine();
/*
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 56 52 53 49 3 77
2 49 69 48 48 48 48 48 65 66 54 53 3 69
2 49 69 48 48 48 48 48 65 66 54 53 3 69
*/
string str = @"168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 56 52 53 49 3 77 2 49 69 48 48 48 48 48 65 66 54 53 3 69 168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 65 66 54 53 3 69";
Regex reg = new Regex(@"168(?:(?!\b2\b).)*");
MatchCollection match = reg.Matches(str);
string[] first = new string[match.Count];
for (int i = 0; i < match.Count; i++)
{
first[i] = match[i].ToString();
Console.WriteLine(match[i]);
}
string result = string.Join(" ", str.Split(first, StringSplitOptions.RemoveEmptyEntries));
reg = new Regex(@"2(?:(?!\b3\b).)*3\s\d+\s?");
foreach (Match m in reg.Matches(result))
Console.WriteLine(m.Value);
Console.ReadLine();
string str = @"168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 56 52 53 49 3 77 2 49 69 48 48 48 48 48 65 66 54 53 3 69 168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 65 66 54 53 3 69";
Regex reg = new Regex(@"168(?:(?!\b2\b).)*");
string first = string.Empty;
foreach (Match m in reg.Matches(str))
{
first = m.Value;
Console.WriteLine(first);
}
string result = string.Join(" ", str.Split(new string[] { first }, StringSplitOptions.RemoveEmptyEntries));
reg = new Regex(@"2(?:(?!\b3\b).)*3\s\d+\s?");
foreach (Match m in reg.Matches(result))
Console.WriteLine(m.Value);
Console.ReadLine();
/*
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 56 52 53 49 3 77
2 49 69 48 48 48 48 48 65 66 54 53 3 69
2 49 69 48 48 48 48 48 65 66 54 53 3 69
*/
void Main()
{
string str="168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 56 52 53 49 3 77 2 49 69 48 48 48 48 48 65 66 54 53 3 69 168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8 2 49 69 48 48 48 48 48 65 66 54 53 3 69";
string[] s=Regex.Split(str,@"(168\s129(?:\s\d+){11}\s48\s49\s53\s48\s1\s0\s\d+\s)");
Console.WriteLine(s);
}
string str = @"168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 56 52 53 49 3 77
2 49 69 48 48 48 48 48 65 66 54 53 3 69
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 65 66 54 53 3 69 ";
string[] result = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
Console.WriteLine(s);
}
Console.ReadLine();
string str = @"168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 56 52 53 49 3 77
2 49 69 48 48 48 48 48 65 66 54 53 3 69
168 129 48 48 48 48 48 48 48 48 53 49 50 48 49 53 48 1 0 8
2 49 69 48 48 48 48 48 65 66 54 53 3 69 ";
Regex regex = new Regex("^.*$", RegexOptions.Multiline);
Match mc = regex.Match(str);
while (mc.Success)
{
Response.Write(mc.Value + "<br/>");
mc = mc.NextMatch();
}