简体   繁体   English

从C#Windows窗体的文本文件中提取数字

[英]Extracting Number from a Text File in C# Windows Form

I have written a code for extracting number from a text file using a windows From. 我已经编写了使用Windows From从文本文件中提取数字的代码。 Problem is that, Output Occurs in a partial way. 问题是,输出是部分发生的。 Either the First Line is Printing or the Last Line. 第一行是“打印”或最后一行。 I want all the line that is containing the number 我想要所有包含数字的行

(i.e) If the text file contains,
Auto 2017
Mech 2056
CSE 2016

I want only those 2017, 2056, 2016 to be printed. 我只希望打印那些2017、2056、2016。

Here is the code: 这是代码:

private void button1_Click(object sender, EventArgs e)
{
   string infile = textBox1.Text;
   StreamReader sr = new StreamReader(infile);
   string allDetails = File.ReadAllText(infile);
   string result = Regex.Match(allDetails, @"\d+").Value;
   richTextBox1.Text = result.ToString();
}

You are try to grab numeric value. 您正在尝试获取数值。 Regex.Matches Will help you to solve your problem. Regex.Matches将帮助您解决问题。

Below is simplified code. 下面是简化的代码。

 private void button1_Click(object sender, EventArgs e)
 {          
    string filedetails = File.ReadAllText(textBox1.Text);
    var regexCollection = Regex.Matches(filedetails, @"\d+");
    foreach (Match rc in regexCollection)
       richTextBox1.AppendText(rc.Value + ",");
 }

You can do this way, if you want output 2017,2056,2016 如果要输出2017,2056,2016 ,可以使用这种方法

private void button1_Click(object sender, EventArgs e)
{
     string infile = textBox1.Text;
     string[] lines = System.IO.File.ReadAllLines(infile);
     string temp = "";
     int i = 0;
     foreach (string line in lines)
     {
         string result = Regex.Match(line, @"\d+").Value;
         if (i == 0)
         {
            temp = result;
         }
         else
         {
            temp = temp + "," + result;
         }
         i++;
    }
    richTextBox1.Text = temp;
}

or if you want single value 2017 2056 2016 then 或者如果您想要单一值2017 2056 2016

private void button1_Click(object sender, EventArgs e)
{
     string infile = textBox1.Text;
     string[] lines = System.IO.File.ReadAllLines(infile);
     foreach (string line in lines)
     {
         string result = Regex.Match(line, @"\d+").Value;
         richTextBox1.Text = result ;
     }
}

You need to use the method. 您需要使用该方法。 Regex.Matches . Regex.Matches Matches method searches the specified input string for all occurrences of a regular expression. Matches方法在指定的输入字符串中搜索所有出现的正则表达式。

Match method returns the first match only, Subsequent matches need to be retrieved. Match方法仅返回第一个匹配项,需要检索后续匹配项。

private void button1_Click(object sender, EventArgs e)
{
   string infile = textBox1.Text;
   StreamReader sr = new StreamReader(infile);
   string allDetails = File.ReadAllText(infile);
   var regexMatchCollection = Regex.Matches(allDetails, @"\d+");
   foreach(Match mc in regexMatchCollection)
   { 
     richTextBox1.AppendText(mc.Value);
     richTextBox1.AppendText(",");
   }

}

Without using Regex ,below is the simplified code 不使用正则Regex ,下面是简化代码

private void button1_Click(object sender, EventArgs e)
{
   StringBuilder numbers = new StringBuilder();
   string allDetails = File.ReadAllText(textBox1.Text);
   foreach(string word in allDetails.Split(' '))
   {
       int number;
       if(int.TryParse(word, out number))
       {
            numbers.Append(number);
            numbers.Append(",");
       } 
   }
   richTextBox1.Text = numbers.Trim(',');
}

test.txt has test.txt有

Auto 2017
Mech 2056
CSE 2016

in the following program File.ReadAllLines will store every line in a string array separately.Then we will use foreach loop to read single line at a time and store the extracted numbers in list eg 2017 and finally with string.Join we will join the array and separate each word with "," and save it in string 在下面的程序File.ReadAllLines中将每一行分别存储在一个string array我们将使用foreach loop读取一行,并将提取的数字存储在列表中(例如2017年),最后存储在string.Join加入后,我们将加入该数组并用","分隔每个单词并将其保存在字符串中

List<string> list = new List<string>();
            var textfile = File.ReadAllLines(@"D:\test.txt");
            foreach (var line in textfile)
            {
            string result = Regex.Match(line, @"\d+").Value;
                list.Add(result);
            }
            string numbers = string.Join(",",list.ToArray());

the output value will be 输出值将是

2017,2056,2016
private void button1_Click(object sender, EventArgs e)
{
            string infile = textBox1.Text;
            StreamReader sr = new StreamReader(infile);
            string allDetails = File.ReadAllText(infile);
            string result = string.Empty;
            foreach (var item in Regex.Matches(allDetails, @"\d+"))
            {
                result = result + item.ToString() + ",";
            }

            richTextBox1.Text = result.TrimEnd(',');
}
 static void Main(string[] args)
    {

        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\admin\Desktop\ConsoleApplication1\ConsoleApplication1\txtFile.txt");
        List<string> Codelst = new List<string>();
        foreach (var item in lines)
        {
            var a= Regex.Match(item, @"\d+").Value;
            Codelst .Add(a);
        }
        var r = Codelst;
    }

Output is like this: 输出是这样的: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM