谢谢拉
如替换 "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";中的four,而ccfour ,ddffdfourdf, fourr
,fourU ,Four 不做替换,而four 和four() 都替换的
如替换 "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";中的four,而ccfour ,ddffdfourdf, fourr
,fourU ,Four 不做替换,而four 和four() 都替换的
解决方案 »
- 寻找C#高手帮我看看socket通信问题,搞了一天了。。
- TextBook前台显示文本,后台绑定值
- 如何将DataTable转化为结果集
- 急!c#调用com异常
- panel的用法,帮我看一下
- 如何传值 修改listBOX选中项的值?
- 100分大放松!!如何让listview中任意一个单元格中都能获得焦点?并且能修改其中得数据?
- 制作安装程序时需要把sql脚本打包,sql脚本中有中文打包后,变成黑方块了,请问应该怎样解决???急
- C#用新线程创建的窗口总是为响应
- 问题是,我到什么地方去找C#的编译器和环境啊?
- sql语句问题:怎样求得当天增加的数目?
- 急,哪位用过.net打包工具的?帮我,急,在线等
s = Regex.Replace(s, @"\bfour\b", "six");
Console.WriteLine(s);
using System.Text.RegularExpressions;private void button1_Click(object sender, System.EventArgs e)
{
string s=@"four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
string myreg=@"\bfour(\(\)){0,1}\b";
string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo));
this.textBox1.Text=a;
} private string mydo(Match m)
{
string a=m.Value;
if(a.IndexOf("()")>0)
return "six()";
else
return "six";
}
Text= Regex.Replace(s, Pattern,"six"); 同意楼上的做法,\b是字符边界,而在这里只要控制其边界就对了!
{
string s=@"four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
string myreg=@"\bfour\b";
string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo));
this.textBox1.Text=a;
} private string mydo(Match m)
{
string a=m.Value;
return "six";
}
{
if(MainString.Length > 0)
{
MainString = MainString.Replace(OldString,NewString);
}
return MainString;
}
//MainString 为主字符串
//OldString 为要替换的字符串
//NewString 为要替换成的新串
测试:
string RepString="four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
RepString = ReplaceString(RepString,"four ", "six ");
//RepString = six four{ ccsix ddffdfourdf score and seven fourr six four() fourU years Four ago
RepString = ReplaceString(RepString,"four()", "six()");
//“six six{ ccfour ddffdfourdf score and seven fourr six six() fourU years Four ago”
//注意输入的字符串的写法
但\bfour\b
是不会匹配_*four_* 中的"four"的.此贴最简捷的正则表达式写法怎么写?
s = Regex.Replace(s, @"(?<![a-zA-Z0-9])four(?![a-zA-Z0-9])", "six");
Console.WriteLine(s);