Click here to Skip to main content
15,887,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I have a small question.

I'm making a code editor (not for a big programming language) and I need some help on splitting the opened file.
The said file has such structure:
#Comments may occur in the code
["values", other values, [more values]" more values],

This was just one tuple of a file that may contain a thousand.

It would look like this:
["values", other values, [more values], more values],
["values", other values, [more values]" more values],
["values", other values, [more values], more values],

But there might be new lines throughout the code, such as:
["values", 
other values, [more 
values], more values],


The whole point is to print every value in a text box and include writing options, but for now my only problem is splitting the tuples correctly.

Could anyone give me hints on how to split such a thing properly? I'll be very, very grateful.
Posted
Comments
volksgrenadier128 11-Jun-10 15:40pm    
You can try by adding an "end tuple" character something like this:

["values", other values, [more values], more values];

Then parse it and add the text to your control.

Looking at the examples you have provided, it looks like the tuples can be split on;

],[


In which case i would give that a bash.
 
Share this answer
 
Comments
LookSharp 11-Jun-10 19:24pm    
It would be nice if it were that simple, but his tuples' contents can also be wrapped in [] so a ],[ sequence can appear within a tuple as well as between them.

The examples all show the first (and only the first) member of each tuple being a quoted string, so perhaps ],[" is the separator to match on. Unfortunately, since the quoted string is 'values' I suspect that any value can be quoted.
User970 12-Jun-10 5:25am    
Only the first one can be quoted. The ],[" separator is interesting, I'd really like to try it out, but whenever I try to use Regex.Split(file, "],["); I get an error (argumentExpetion - Unterminated [] set). Plus, I don't know how to put quotes as a separator... Any help on this?
(I know, stupid question, but I'm new to C#)

EDIT: After some googling, I managed to find it out. Look at my answer.
User970 16-Aug-10 10:55am    
Reason for my vote of 5
It helped me answer myself
Given the assumptions that (1) any value can be optionally quoted (so the presence of quotes cannot be used as a marker) (2) brackets ([]) can be nested to an arbitrary depth, then you need to implement a parser of some kind that is able to detect commas that are not within brackets. For example:

// Returns a List<> of the positions of the record separators (commas that are not enclosed by brackets)
public List<int> ParseNestedBrackets(string inputText)
    {
    List<int> separators = new List<int>();

    int depth = 0;
    int length = inputText.Length;      // Cache the length to save a method call for each interation of the loop
    for (int pos = 0; pos < length; ++pos)
        {
        switch (inputText[pos])
            {
        case '[':
                depth += 1;
                break;
        case ',':
                if (depth == 0)
                    separators.Add(pos);
                break;
        case ']':
                depth -= 1;
                break;
        default:
            break;
            }
        }

        return separators;
    }
 
Share this answer
 
I did it! I had to use this:
//Debug
            filearray = Regex.Split(file, @"\[""");
            foreach (string item in filearray)
            {
                MessageBox.Show(item);
            }

but it captures the [". This works for now. Thank you all!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900