求正则表达式:匹配字符串中的重复字符 例如: abcad efbgh匹配后的结果应该是: ab因为只有ab在原字符串中有重复出现请指导... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 (\w)(?=.*?\1)string str="abcad efbgh";string result=string.Empty;Regex reg=new Regex(@"(\w)(?=.*?\1)");foreach(Match m in reg.Matchs(str)){ result+=m.Value;} 很感谢 min_jie你给的答案基本上已经满足了我的要求.但是这种情况: abdcaaf用你的表达式好像会出来两个:aa也就是说a被匹配了两次能不能只出来一次呢?还有, ab cdefg ab abgf能不能把其中的ab匹配出来呢? private string GetRepeatString(string str){ string result=string.Empty; if(str.Length==0) return result; while(str.Length>0) { string temp=str[0].ToString(); int len=str.Length; str=str.Replace(temp,""); if(len-str.Length>1) result+=temp; } return result;} 至少一个参数没有被指定值。 fckeditor问题,帮看下。 .net源码保护加密 框架下拉菜单myMenu的问题 阅读器关闭时尝试调用 Read 无效,如何解决 VS启动浏览的时候如何设置其他浏览器? 请问各位高人,我想把两个音频文件组合成一个文件,该怎么办呀? vs2012自带的menu控件如何和数据库绑定? 这个问题如何解决 江雨等高手,帮帮忙吧,实在是自己搞不定了阿 后台创建控件在客户端显示。并在后台获得控件的问题???研究了一天了(虚脱了) 关于连接excel
string result=string.Empty;
Regex reg=new Regex(@"(\w)(?=.*?\1)");
foreach(Match m in reg.Matchs(str))
{
result+=m.Value;
}
你给的答案基本上已经满足了我的要求.
但是这种情况: abdcaaf
用你的表达式好像会出来两个:
a
a
也就是说a被匹配了两次
能不能只出来一次呢?还有, ab cdefg ab abgf
能不能把其中的ab匹配出来呢?
{
string result=string.Empty;
if(str.Length==0) return result;
while(str.Length>0)
{
string temp=str[0].ToString();
int len=str.Length;
str=str.Replace(temp,"");
if(len-str.Length>1) result+=temp;
}
return result;
}