Split string, create group 1 to n relationship, possiblities

Markus Freitag 3,786 Reputation points


I have an input of string and need to derive a list, dictionary from it. What is the best and easiest way to do this?

Maybe split or better RegEx?

/// Input
// objects="Object0-EAN13CODE^27730180^Object1-TEXT^104612^Object2-TEXT^27730180^Object3-TEXT^AT1^" 

// Output
Object0 Type=RECEIVER Value=27730180
Object1 Type=TEXT Value=104612
Object2 Type=TEXT Value=27730180

Key         Value
Object0     type=EAN13CODE Value=27730180
Object1     type=TEXT Value=104612
Object2     type=TEXT Value=27730180
Object3     type=TEXT Value=AT1
   Input is a loop, can be until 10 items.

1 -> objects="Object0-EAN13CODE^27730180^Object1-TEXT^104612^Object2-TEXT^27730180^Object3-TEXT^AT1^" 
2 -> objects="Object0-EAN13CODE^27730181^Object1-TEXT^104612^Object2-TEXT^27730181^Object3-TEXT^AT1^" 
3 -> objects="Object0-EAN13CODE^27730182^Object1-TEXT^104612^Object2-TEXT^27730182^Object3-TEXT^AT1^" 

Maybe better

Group         Value
   Key         Value
   object0     type=EAN13CODE Value=27730180
   object1     type=TEXT Value=104612
   object2     type=TEXT Value=27730180
   object3     type=TEXT Value=AT1
   Key         Value
   object0     type=EAN13CODE Value=27730181
   object1     type=TEXT Value=104612
   object2     type=TEXT Value=27730181
   object3     type=TEXT Value=AT1
   Key         Value
   object0     type=EAN13CODE Value=27730182
   object1     type=TEXT Value=104612
   object2     type=TEXT Value=27730182
   object3     type=TEXT Value=AT1

The goal is to create a new string.

   newValue = "27730180;27730181;27730182;104612;27730180;27730181;27730182;AT1";

The rule is to list all object0 elements one after the other, then object1, object2 if they are the same do not add. In the sample you can see 104612 und AT1 appears only once.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,209 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 6,436 Reputation points MVP
    string[] inputs ={
    Regex reg = new Regex(@"(?<=(^|\^))Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^");
    var dictionaries = inputs.Select(input =>
            .ToDictionary(m => int.Parse(m.Groups["NUM"].Value), m => new { type = m.Groups["KEY"].Value, value = m.Groups["VALUE"].Value })
    if (dictionaries.Length == 0)
    for (int i = 0; i < inputs.Length; i++)
        var dic = dictionaries[i];
        Console.WriteLine(i + 1);
        foreach (var kv in dic)
            Console.WriteLine($"\tobject{kv.Key}\t\ttpe={kv.Value.type} value={kv.Value.value}");
    int[] keys = dictionaries.SelectMany(dic => dic.Keys).Distinct().OrderBy(_ => _).ToArray();
    int keymin = keys.First();
    IEnumerable<string> ie 
        = dictionaries
        .Select(dic => dic[keymin].value)
                .SelectMany(key => dictionaries
                                    .Where(dic => dic.ContainsKey(key))
                                    .Select(dic => dic[key].value)
    string result = string.Join(";", ie);
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful