string text = "英格兰{#比分1#}击败了斯洛文尼亚,加纳{#比分2#}负于德国";
请问怎样用正则将 {# #} 里的 比分1 和 比分2 取出来呢,
并且使用新的值替换里面的内容,替换之后的结果为:string text = "英格兰1比0击败了斯洛文尼亚,加纳0比1负于德国";谢谢。
请问怎样用正则将 {# #} 里的 比分1 和 比分2 取出来呢,
并且使用新的值替换里面的内容,替换之后的结果为:string text = "英格兰1比0击败了斯洛文尼亚,加纳0比1负于德国";谢谢。
解决方案 »
- Windows服务的问题请帮忙,在线等。谢谢。
- 我这个菜鸟快死了,救救我吧
- 各位大侠来帮忙,坐标轴的拖动,在线等待!!!
- 100分求C#描述的MapInfo教程
- 关于freetextbox1.63图片上传至数据库的一个小问题
- 100分解决小问题
- 请问这个关于excel的例子怎么运行?
- 请问:在文本框里输入密码时,要求显示出来的是“*****”,怎样实现?谢谢!!!
- OleDbCommandBuilder是个什么东西,是干什么用的?怎么使用?
- 50分找小巧好用的C#编辑器
- backgroundwork.DoWork事件里能否用 this..Invoke()来修改主线程里的控件?
- MainMenu的属性
Hashtable hs = new Hashtable();
hs.Add("比分1", "1比0");
hs.Add("比分2", "0比1");
Regex reg = new Regex(@"\{#(比分\d+)#}");
string result = reg.Replace(text, delegate(Match m) { return hs[m.Groups[1].Value].ToString(); });
richTextBox2.Text = result;
Regex reg = new Regex(@"(?is)英格兰(?<score1>\d{1,2}比\d{1,2}).*斯洛文尼亚.*加纳(?<score2>\d{1,2}比\d{1,2}).*德国");
Match m = reg.Match(text);
if (m.Success)
{
Console.WriteLine(m.Groups["score1"].Value);
Console.WriteLine(m.Groups["score2"].Value);
}
Hashtable hs = new Hashtable();
hs.Add("比分1", "1比0");
hs.Add("比分2", "0比1");
Regex reg = new Regex(@"\{#(比分\d+)#}");
string result = reg.Replace(text, delegate(Match m) { return hs[m.Groups[1].Value].ToString(); });
richTextBox2.Text = result;
{
public partial class Program
{
[STAThreadAttribute]
static void Main(string[] args)
{
string text = "英格兰{0:score1}击败了斯洛文尼亚,加纳{0:score2}负于德国";
text = string.Format(new ScoreFormater(), text, "1比0", "0比1");
Console.WriteLine(text);
Console.ReadKey();
} public class ScoreFormater : IFormatProvider, ICustomFormatter
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
return arg.ToString();
} public object GetFormat(Type formatType)
{
return formatType == typeof(ICustomFormatter) ? this : null;
}
}
}
}
string a1 = null;
string a2 = null;
a1 = text.split(",")(0);
a2 = text.split(",")(1);
for (int i = 0; i <= 30; i++) {
a1 = a1.replace("比分" + i, i + "比0");
a2 = a2.replace("比分" + i, "0比" + i);
}
Console.WriteLine((a1 + "," + a2).Replace("(#", "").Replace("#)", ""));
string a1 = text.Split(",")[0];
string a2 = text.Split(",")[1];
for (int i = 0; i <= 30; i++) {
a1 = a1.Replace("比分" + i, i + "比0");
a2 = a2.Replace("比分" + i, "0比" + i);
}
Interaction.MsgBox((a1 + "," + a2).Replace("{#", "").Replace("#}", ""));
Regex.Replace(input, "{#([^#{}]+)#}", "$1");
//结果:英格兰比分1击败了斯洛文尼亚,加纳比分2负于德国