java正则表达式C#

最护眼的颜色

#CFE8CC

207232204

//Java版本
//替换字符串s中的所有字母(这里替换为“”)
public static String getNumberInfo(StringBuffer s) {
	String ptn = "[a-zA-Z]+";
	Pattern p = Pattern.compile(ptn, Pattern.DOTALL);
	Matcher m = p.matcher(s);
	while (m.find()) {
		s = s.replace(m.start(0), m.end(0), "");
		m = p.matcher(s);
	}
	return s;
}


//C# 版本
public Form1()
{
	InitializeComponent();

	StringBuilder s = new StringBuilder(@"haohao,9.36,ssf
,123, good ,421f,ffd,fz,5.26,,,asfggds,13123");
	Test2(s);
}

private void Test2(StringBuilder s)
{
	String ptn = "[a-zA-Z]+";
	MatchCollection matchCol = Regex.Matches(s.ToString(), ptn, RegexOptions.Multiline);
	for (int i = matchCol.Count - 1; i >= 0; i--)
	{
		Match item = matchCol[i];
		s.Remove(item.Index, item.Length);
	}       
	txtScan.AppendText(string.Format("结果:{0}\r\n", s.ToString()));
}

相关推荐