Dooms
Dooms

Reputation: 25

How to parse this from a text file

I am having a problem parsing the below example from a text file. What I am attempting to do is read the entire text file and then split the file up so each number is an individual element.

So it goes from this:

[466 474 482]

[24 8 29]

[50 46 3]

To this

[466], [474], [482], [24, [8], [29], [50], [46], [3]

My plan was to read the text by using ReadAllLines and then split the numbers individually using the Split method like so

string[] textFile = File.ReadAllLines(@"path");
string[] textFile2 = { };

for (int i = 0; i < textFile.Length; i++)
{
     textFile2 = textFile[i].Split(' ');
}

foreach (string number in textFile2)
{
     Console.Write(number + " ");
}

but I end up with this [50] [46] [3].

Upvotes: 2

Views: 167

Answers (3)

Cavan Page
Cavan Page

Reputation: 535

You can concatenate input array and then split by char.

string[] textFile = File.ReadAllLines(@"path");
string concatInput = String.Join(" ", textFile);
string[] textFile2 = concatInput.Split(' ');

foreach (string number in textFile2)
{
     Console.Write(number + " ");
}

if you would like to remove the brackets [] you can do something like this...

string concatInput = String.Join(" ", textFile).Replace('[', ' ').Replace(']', ' ');
string[] textFile2 = concatInput.Split(' ').Where(x => x != "").ToArray();

Upvotes: 0

hardyVeles
hardyVeles

Reputation: 1132

I prefer to use Regex for such stuff.

Make sure you call Regex namespace at top of your code:

using System.Text.RegularExpressions;

Define a Regex instance as static field - it is good idea if you'll use this in high frequency.

public static Regex extractNumberRegex = new Regex("([\\d]+)[\\s]?");

The example below defines extension methods within a static class. First, let's extract the numbers from a String.

public static List<String> extractNumbers(this String source)
{
   List<String> output = new List<string>();

   MatchCollection mchCol = extractNumberRegex.Matches(source);
   foreach (Match mch in mchCol)
   {
       output.Add(mch.Value);
   }
   return output;
}

Your main method (here as extension too):

public static List<String> extract(this String path)
{
    String[] lines = File.ReadAllLines(path);
    foreach (String ln in lines)
    {
       List<String> numers = ln.extractNumbers();
       String reformatLine = "";
       foreach (String num in numers)
       {
           reformatLine = reformatLine + "[" + num + "] ";
       }
       reformatLine = reformatLine.Trim();

       Console.WriteLine(reformatLine);
    }

}

Upvotes: 0

Anderson Pimentel
Anderson Pimentel

Reputation: 5757

You are replacing textFile2 content for every line you read. Try using a List<string> instead.

string[] textFile = File.ReadAllLines(@"path");
List<string> textFile2 = new List<string>();

for (int i = 0; i < textFile.Length; i++)
{
    textFile2.AddRange(textFile[i].Split(' '));
}

foreach (string number in textFile2)
{
    Console.Write(number + " ");
}

If your file really looks like that, with square brackets and blank lines, this may help you: https://dotnetfiddle.net/fzBU4W

Upvotes: 2

Related Questions