string x = "used:10246 free:20489";
Regex r = new Regex(@"(?<!used:)\d{4}");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//第一次匹配“sed:10246",其中反向负声明匹配为:”sed:1",后面的数字为匹配返回结果
//第二次匹配"free:2048",其中反向负声明匹配为:”free:",后面的数字为匹配返回结果
//包含两个匹配的结果:0246,2048
MatchCollection mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"\d{4}(?<!used:)");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//该正在表达式可替换为@"\d{4}",因为符合该条件的内容边界点的左侧肯定不是"used:"。
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"(?!used:)\d{4}");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//r = new Regex(@"\d{4}");
//该处的表达式与"\d{4}"等价,匹配项左边界的右边为4个数字,肯定不是"used:"。
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"\d{4}(?!used:)");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//该表达式表示匹配项右边界的右边不能为"used:",
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
x = "aaa ffffff 999999999";
r = new Regex(@"(\w)((?=\1\1\1)(\1))+");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//匹配结果为:ffff,9999999
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
Regex r = new Regex(@"(?<!used:)\d{4}");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//第一次匹配“sed:10246",其中反向负声明匹配为:”sed:1",后面的数字为匹配返回结果
//第二次匹配"free:2048",其中反向负声明匹配为:”free:",后面的数字为匹配返回结果
//包含两个匹配的结果:0246,2048
MatchCollection mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"\d{4}(?<!used:)");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//该正在表达式可替换为@"\d{4}",因为符合该条件的内容边界点的左侧肯定不是"used:"。
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"(?!used:)\d{4}");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//r = new Regex(@"\d{4}");
//该处的表达式与"\d{4}"等价,匹配项左边界的右边为4个数字,肯定不是"used:"。
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
r = new Regex(@"\d{4}(?!used:)");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//该表达式表示匹配项右边界的右边不能为"used:",
//包含两个匹配的结果:1024,2048
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}
x = "aaa ffffff 999999999";
r = new Regex(@"(\w)((?=\1\1\1)(\1))+");
Console.WriteLine();
Console.WriteLine("string:{0} \npatten:{1}", x, r);
//匹配结果为:ffff,9999999
mc = r.Matches(x);
foreach (Match m in mc)
{
Console.WriteLine("index:{0}, Value:{1},length:{2}", m.Index, m.Value, m.Length);
}