1

So I am coding a converter program that convers a old version of code to the new version you just put the old text in a text box and it converts Txt to Xml and im trying to get each items beetween two characters and below is the string im trying to split. I have put just the name of the param in the " " to protect my users credentials. So i want to get every part of code beetween the ","

["Id","Username","Cash","Password"],["Id","Username","Cash","Password"]

And then add each string to a list so it would be like

Item 1
["Id","Username","Cash","Password"]

Item 2
["Id","Username","Cash","Password"]

I would split it using "," but then it would mess up because there is a "," beetween the params of the string so i tried using "],"

string input = textBox1.Text;
string[] parts1 = input.Split(new string[] { "]," }, StringSplitOptions.None);
foreach (string str in parts1)
{
    //Params is a list...
    Params.Add(str);
}
MessageBox.Show(string.Join("\n\n", Params));

But it sort of take the ] of the end of each one. And it messes up in other ways

svick
  • 236,525
  • 50
  • 385
  • 514
Ash Smith
  • 21
  • 2

3 Answers3

1

This looks like a great opportunity for Regular Expressions.

My approach would be to get the row parts first, then get the column parts. I'm sure there are about 30 ways to do this, but this is my (simplistic) approach.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var rowPattern = new Regex(@"(?<row>\[[^]]+\])", RegexOptions.Multiline | RegexOptions.ExplicitCapture);
            var columnPattern = new Regex(@"(?<column>\"".+?\"")", RegexOptions.Multiline | RegexOptions.ExplicitCapture);
            var data = "[\"Id\",\"Username\",\"Cash\",\"Password\"],[\"Id\",\"Username\",\"Cash\",\"Password\"]";
            var rows = rowPattern.Matches(data);
            var rowCounter = 0;
            foreach (var row in rows)
            {
                Console.WriteLine("Row #{0}", ++rowCounter);
                var columns = columnPattern.Matches(row.ToString());
                foreach (var column in columns)
                    Console.WriteLine("\t{0}", column);
            }
            Console.ReadLine();
        }
    }
}

Hope this helps!!

Luc
  • 1,830
  • 2
  • 23
  • 38
1

You can use Regex.Split() together with positive lookbehind and lookahead to do this:

var parts = Regex.Split(input, "(?<=]),(?=\\[)");

Basically this says “split on , with ] right before it and [ right after it”.

svick
  • 236,525
  • 50
  • 385
  • 514
0

Assuming that the character '|' does not occur in your original data, you can try:

input.Replace("],[", "]|[").Split(new char[]{'|'});

If the pipe character does occur, use another (non-occurring) character.

Boluc Papuccuoglu
  • 2,318
  • 1
  • 14
  • 24
  • Am i right in thinking this only splits the first | ? Is there a way to do it more than once because there is going to be able 100+ of these – Ash Smith Aug 20 '14 at 16:28
  • 1
    @AshSmith No, `Replace()` replaces all occurrences, not just the first one. – svick Aug 20 '14 at 16:30
  • No, all the occurrences of '],[' will be replaced: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=en-US&k=k(SYSTEM.STRING.REPLACE);k(TargetFrameworkMoniker-%22SILVERLIGHT%2cVERSION%3dV5.0%22);k(DevLang-CSHARP)&rd=true – Boluc Papuccuoglu Aug 20 '14 at 16:30