1、调用正则库:using System.Text.RegularExpressions;
2、代码如下
private void RegexMatches()
{
string str = richTextBox1.Text;//获取输入框内容
//正则提取所有数字
MatchCollection result = Regex.Matches(str, @"[\-\d\.]+");
richTextBox3.AppendText("使用正则表达式提取数字\r\n");
//循环打印所有数字,result.Count是提取的总数量
for(int i=0;i<result.Count;i++)
{
richTextBox3.AppendText(result[i].Value+"\r\n");
}
}
//按钮触发事件
private void button1_Click(object sender, EventArgs e)
{
RegexMatches();
}
3、效果如下:

本文介绍了如何使用C#的正则表达式功能从输入框中提取所有数字,通过RegexMatches方法实现,并展示了实际操作步骤和效果。
1710

被折叠的 条评论
为什么被折叠?



