简体   繁体   中英

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. 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.

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.

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

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

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 . Matches method searches the specified input string for all occurrences of a regular expression.

Match method returns the first match only, Subsequent matches need to be retrieved.

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

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

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

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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