string s = @"@xxx@xxxxx@@@yy@yyyyyyyyyy@xxx@@xxxxxxx @yy@yyyyy@xxxxxxxx @yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy @xxx@xxxxx@xxxxxxxxx@xx@xx @yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx"; int i = 1; s = Regex.Replace(s, @"@[^@\n]{2}@[^@\n]{4,}", new MatchEvaluator(delegate(Match m) { return string.Format("%{0}%", i++); })); Console.WriteLine(s);找到一个符合的赋值到一个string[] 然后替换用%1% 找到第二个也赋值到一个string[] 替换用%2% 第三个也也赋值到一个string[] 替换用%3%
s = Regex.Replace(s, @"@[^@\n]{2}@[^@\n]{4,}", new MatchEvaluator(delegate(Match m) { return string.Format("%{0}%", i++); }));这一句得改一下。。正则 @"@[^@\n]{2}@[^@\n]{4,}" 改为 @"@[^@\n]{2}@[^@\n]{5,}"。。大于4个字符的。。
string s = @"@xxx@xxxxx@@@yy@yyyy@xxx@@xxxxxxx @yy@yyyyy@xxxxxxxx @yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy @xxx@xxxxx@xxxxxxxxx@xx@xx @yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx"; int i = 1; List<string> sList = new List<string>(); // 存放结果的集合。。 s = Regex.Replace(s, @"@[^@\n]{2}@[^@\n]{5,}", new MatchEvaluator(delegate(Match m) { sList.Add(m.Value); return string.Format("%{0}%", i++); })); Console.WriteLine(s); i = 1; foreach (string ss in sList) { Console.WriteLine("第{0}个:{1}", i++, ss); }
string result = Regex.Replace(yourStr,@"@[^@]{2}@[^@\s]{4,}","");
void Main()
{
string s=@"@xxx@xxxxx@@@yy@yyyyyyyyyy@xxx@@xxxxxxx
@yy@yyyyy@xxxxxxxx
@yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy
@xxx@xxxxx@xxxxxxxxx@xx@xx
@yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx";
s=Regex.Replace(s,@"@[^@]{2}@[^@]{4,}","");
Console.WriteLine(s);
/*
@xxx@xxxxx@@@xxx@@xxxxxxx
@xxxxxxxx
@xxx@xxxxx@xxxxxxxxx@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx
*/
}
找到一个符合的赋值给一个string[] 然后替换用%1%
找到第二个也赋值给一个string[] 替换用%2%
第三个也也赋值给一个string[] 替换用%3%
这个方法 把@xx@xx 也替换了
void Main()
{
string s=@"@xxx@xxxxx@@@yy@yyyyyyyyyy@xxx@@xxxxxxx
@yy@yyyyy@xxxxxxxx
@yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy
@xxx@xxxxx@xxxxxxxxx@xx@xx
@yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx";
s=Regex.Replace(s,@"@[^@]{2}@[^@]{4,}\b","");
Console.WriteLine(s);
/*
@xxx@xxxxx@@@xxx@@xxxxxxx
@xxxxxxxx@xxx@xxxxx@xxxxxxxxx@xx@xx
@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx */
}
用这方法就可以了 非常感谢但是我想问一下如果想
找到一个符合的赋值到一个string[] 然后替换用%1%
找到第二个也赋值到一个string[] 替换用%2%
第三个也也赋值到一个string[] 替换用%3%
@yy@yyyyy@xxxxxxxx
@yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy
@xxx@xxxxx@xxxxxxxxx@xx@xx
@yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx";
int i = 1;
s = Regex.Replace(s, @"@[^@\n]{2}@[^@\n]{4,}", new MatchEvaluator(delegate(Match m) { return string.Format("%{0}%", i++); }));
Console.WriteLine(s);找到一个符合的赋值到一个string[] 然后替换用%1%
找到第二个也赋值到一个string[] 替换用%2%
第三个也也赋值到一个string[] 替换用%3%
@yy@yyyyy@xxxxxxxx
@yy@yyyyyyyyyyyyyyyy@yy@yyyyyyy
@xxx@xxxxx@xxxxxxxxx@xx@xx
@yy@yyyyyyyyyyyyyy@yyyyyyyyyyyyyyyyyyyyy@xxxxxxxxxxxx";
int i = 1;
List<string> sList = new List<string>(); // 存放结果的集合。。
s = Regex.Replace(s, @"@[^@\n]{2}@[^@\n]{5,}", new MatchEvaluator(delegate(Match m)
{
sList.Add(m.Value);
return string.Format("%{0}%", i++);
}));
Console.WriteLine(s);
i = 1;
foreach (string ss in sList)
{
Console.WriteLine("第{0}个:{1}", i++, ss);
}