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、效果如下: