0

I am having issues creating a class to read and write to an excel file. I need to read in the data from the file (only one column). Then I need to search the string input for square brackets [] and/or parentheses (). If the line contains those, I need to take the information inside those and separate (split) the answers by a forward slash (/). I keep getting errors with the splitting on the variables. Could someone point me in the right direction? Thanks!

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace DataExcelApp
{
    public class ExcelApp
    {
        private static void Main()
        {
            //make connection to document
            var fileName = string.Format("C:/Users/kbangert/Desktop/Karpel/ChargeLanguage.xlsx", Directory.GetCurrentDirectory());
            var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

            //first query to document
            var adapter = new OleDbDataAdapter("SELECT * FROM [ChargeLanguage] WHERE [description] != string.Empty", connectionString);
            var ds = new DataSet();
            adapter.Fill(ds, "descriptions");

            //DataTable data = ds.Tables["descriptions"];
            var data = ds.Tables["descript"].AsEnumerable();

            if (data != null)
            {
                var entry = data.ToString();
                var pattern = @"\[(.*?)\]";
                var matches = Regex.Matches(entry, pattern);

                foreach (Match m in matches)
                {
                    Console.WriteLine(m.Groups[1]);
                }

                string[] words = matches.Split('/');
                foreach (string word in words)
                {
                    Console.WriteLine(word);
                }
            }    
2
  • 3
    I keep getting errors what are they? Runtime exceptions? Compile errors? Where? What have you tried to fix the issues so far? Commented Sep 26, 2013 at 13:14
  • I keep getting an "Cannot resolve symbol - Split" error and it won't compile at all, even without the split portion Commented Sep 26, 2013 at 13:21

1 Answer 1

2

In this line

string[] words = matches.Split('/');

matches is a collection of Match objects. There's no Split method on that.

You probably want to do this:

foreach (Match m in matches)
{
    Console.WriteLine(m.Groups[1]);

     string[] words = m.Groups[1].Value.Split('/');
     foreach (string word in words)
        Console.WriteLine(word);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Is there another way that I could go about filtering out the [] braces and () parentheses and split that up?
Does it work the way I wrote? If so, use this, because it's pretty straight forward. Of course you could write a regular expression that already captures things the way you want.
I get 2 errors - Error 2 'System.Xml.Linq.XElement' does not contain a definition for 'Replace' and no extension method 'Replace' accepting a first argument of type 'System.Xml.Linq.XElement' could be found (are you missing a using directive or an assembly reference?)
And Error - 'System.Data.DataTable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?)
Not in the code you posted and not in the change I suggested, I assume. So for now I guess that my change works. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.