Diapolos 2018-12-10 15:46 采纳率: 60%
浏览 845
已采纳

C#关于正则匹配并提取txt文件中正负小数的问题

C# 我想要提取如图文档里的正负小数存到数组里,代码怎么修改?图片说明

 private void button2_Click(object sender, EventArgs e)
        {
            string f;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                f = openFileDialog1.FileName;               
            }
            string lineread=string.Empty ;
            string curFileName;
            curFileName = openFileDialog1.FileName;
            StreamReader sr = new StreamReader(curFileName);
            double[] wfwys = new double[12];//提取存到这个数组           
            int p = 0;
            while ((lineread = sr.ReadLine()) != null)
            {
                 lineread = sr.ReadLine();
                 lineread = Convert.ToString(lineread);
                if ( ! Regex.IsMatch(lineread, @"[\u4e00-\u9fbb]"))//排除有汉字的行
                {
                    if (Regex.IsMatch(lineread, @"^[\\+\\-]?[\\d]+(\\.[\\d]+)?$"))//排除没有小数的行
                    {
                        Match substring = Regex.Match(lineread, @"^[\\+\\-]?[\\d]+(\\.[\\d]+)?$");
                        wfwys[p] = double.Parse(substring.Value);
                        p++;
                    }
                }

            }
            textBox2.Text = Convert.ToString(wfwys[0]);
            textBox3.Text = Convert.ToString(wfwys[1]);
            textBox4.Text = Convert.ToString(wfwys[2]);
                        。。。。。。
  • 写回答

2条回答

  • Diapolos 2018-12-11 09:53
    关注

    参考网上的资源,最后编译成功了
    string f;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
    
                f = openFileDialog1.FileName;
    
            }
    
            string lineread = string.Empty;
    
            string curFileName;
    
            curFileName = openFileDialog1.FileName;
    
            StreamReader sr = new StreamReader(curFileName);
    
            double[] wfwys = new double[12];
    
            int p = 0;
    
            while ((lineread = sr.ReadLine()) != null)
            {
    
                lineread = sr.ReadLine();
    
                lineread = Convert.ToString(lineread);
    
                MatchCollection mt = Regex.Matches(lineread, @"[\u4e00-\u9fa5]");//这里用Matches匹配,如果有一个汉字,则匹配成功.汉字的unicode编码是4e00到9fa5
    
                if (mt.Count == 0)
                { //如果没有汉字的行
    
                    MatchCollection mc = Regex.Matches(lineread, @"[+\-]?\d+\.\d+");//这里用Matches匹配,如果有小数,则匹配成功.因为小数不是一整行,所以去掉^和$
    
                    if (mc.Count > 0)
                    {//如果有小数的行
    
                        //Match substring = Regex.Match(lineread, @"[+-]?\d+\.\d+");//这里去掉这句,因为上面有匹配结果mc了
    
                        foreach (Match m in mc)
                        {  //这里遍历mc,因为一行只有一个小数,所以一行存一个小数到wfwys数组
    
                            wfwys[p] = double.Parse(m.Groups[0].Value); //这里把substring.Value改成m.Groups[0].Value
    
                            p++;
    
                        }
    
                    }
    
                }
    
            }
            textBox2.Text = Convert.ToString(wfwys[0]);
                    textBox3.Text = Convert.ToString(wfwys[1]);
                    textBox4.Text = Convert.ToString(wfwys[2]);
                    textBox5.Text = Convert.ToString(wfwys[3]);
    
    
    

    正则用起来真的好深奥啊......

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)