0

This is how my current string looks like:

Data={Person, Id, Destination={Country, Price}}

It may also look like this

Data={Person, Id, Specification={Id, Destination={Country, Price}}}

What I need is a way for me to search through the entire string and be able to get:

Search for Data, then get the entire data between the first and last bracket Data={**GET THIS**}

Then with the data obtained I should be able to get:

Person, Id, then if I find Specification do the same as with data, get the contents inside until the last bracket, and do the same then for Destination.

Any clues what should I do? I'm using C# for this.

4
  • 1
    Does not look like JSON data at all, unless perhaps you mean that it similarly contains braces. A JSON library won't help here. Commented Oct 12, 2013 at 7:15
  • Isn't there a way really to get the first bracket index and last one index inside the string, then get the entire data between these indexes?, and do the same for any other bracket inside a bracket? Commented Oct 12, 2013 at 7:19
  • Looks to me like you just need to strip the first 6 and last characters... is that all you need? Commented Oct 12, 2013 at 7:33
  • I can strip the first 6 characters, not for the last ones though as the interface varies, that's why I need to get data inside each bracket "{someone, someone={someone}}" Commented Oct 12, 2013 at 7:37

2 Answers 2

1

Function to retrieve everything between the curly braces:

public string retrieve(string input)
{
    var pattern = @"\{.*\}";
    var regex = new Regex(pattern);
    var match = regex.Match(input);
    var content = string.Empty;
    if (match.Success)
    {
        // remove start and end curly brace
        content = match.Value.Substring(1, match.Value.Length - 2);
    }
    return content;
}

Then using the function retrieve the contents:

var input = @"Data={Person, Id, Specification={Id, Destination={Country, Price}}}";
var content = retrieve(input);
Console.Out.WriteLine(content);
if (!string.IsNullOrEmpty(content))
{
    var subcontent = retrieve(content);
    Console.Out.WriteLine(subcontent);
    // and so on...
}

The output is:

Person, Id, Specification={Id, Destination={Country, Price}}
Id, Destination={Country, Price}

You can not use string.Split(',') to retrieve Person and Id because it would also split the strings between the brackets and you do not want this. Instead use as suggested string.IndexOf two times from the correct position and you will have the substrings correctly:

// TODO error handling
public Dictionary<string, string> getValues(string input)
{
    // take everything until =, so string.Split(',') can be used
    var line = input.Substring(0, input.IndexOf('='));
    var tokens = line.Split(',');
    return new Dictionary<string, string> { { "Person" , tokens[0].Trim() }, { "Id", tokens[1].Trim() } };
}

The function should be used on the retrieved content:

var input = @"Data={Person, Id, Specification={Id, Destination={Country, Price}}}";
var content = retrieve(input);
var tokens = getValues(content);
Console.Out.WriteLine(string.Format("{0} // {1} // {2}", content, tokens["Person"], tokens["Id"]));
if (!string.IsNullOrEmpty(content))
{
    var subcontent = retrieve(content);
    Console.Out.WriteLine(subcontent);
    var subtokens = getValues(subcontent);
    Console.Out.WriteLine(string.Format("{0} // {1} // {2}", subcontent, subtokens["Person"], subtokens["Id"]));
}

And the output is:

Person, Id, Specification={Id, Destination={Country, Price}} // Person // Id
Id, Destination={Country, Price}
Id, Destination={Country, Price} // Id // Destination
Sign up to request clarification or add additional context in comments.

Comments

0

You could try something like this -

string str = "Data={Person, Id, Specification={Id, Destination={Country, Price}}}";
string[] arr = str.Split(new char[]{'{','}','=',','},StringSplitOptions.RemoveEmptyEntries).Where(x => x.Trim().Length != 0).Select(x => x.Trim()).ToArray();
foreach(string s in arr)
    Console.WriteLine(s);

Where(x => x.Trim().Length != 0) part is used to remove strings that only contains spaces.

Select(x => x.Trim()) part is used to remove leading and trailing spaces of all the substrings.

the result comes out to be

Data
Person
Id
Specification
Id
Destination
Country
Price

so now you can iterate through the array and parse the data. basic logic would be

if(arr[i] == "Data") then next two items would be person and id

if(arr[i] == "Destination") then next two items would be id and destination

if(arr[i] == "specification") then next two items would be country and price

2 Comments

Thanks for your help, but there's something I would like to ask, the code you supplied works really the way I need it to, but is there a way that for example my data: Data=(Id Specification=ANYTHING, id) I mean, be able to get all text if after Id there is no comma that isn't inside a " but a space character, get all that text until it's it hits a bracket or a comma?
@RaúlSanpedro, I think your problem has more to do with finding matching parenthesis and then parsing the data between them. Check out this link which discusses on how to find matching parenthesis.

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.