Split string, create group 1 to n relationship, possiblities

Markus Freitag 3,786 Reputation points
2024-03-01T17:59:42.71+00:00

Hello,

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
         
Problem 
   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
1           
   Key         Value
   object0     type=EAN13CODE Value=27730180
   object1     type=TEXT Value=104612
   object2     type=TEXT Value=27730180
   object3     type=TEXT Value=AT1
------------
2           
   Key         Value
   object0     type=EAN13CODE Value=27730181
   object1     type=TEXT Value=104612
   object2     type=TEXT Value=27730181
   object3     type=TEXT Value=AT1
----------
3           
   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.

C#
C#
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
    2024-03-02T05:09:47.6166667+00:00
    string[] inputs ={
        "Object0-EAN13CODE^27730180^Object1-TEXT^104612^Object2-TEXT^27730180^Object3-TEXT^AT1^",
        "Object0-EAN13CODE^27730181^Object1-TEXT^104612^Object2-TEXT^27730181^Object3-TEXT^AT1^",
        "Object0-EAN13CODE^27730182^Object1-TEXT^104612^Object2-TEXT^27730182^Object3-TEXT^AT1^",
    };
    
    Regex reg = new Regex(@"(?<=(^|\^))Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^");
    
    var dictionaries = inputs.Select(input =>
            reg.Matches(input)
            .OfType<Match>()
            .ToDictionary(m => int.Parse(m.Groups["NUM"].Value), m => new { type = m.Groups["KEY"].Value, value = m.Groups["VALUE"].Value })
            ).ToArray();
    
    if (dictionaries.Length == 0)
    {
        return;
    }
               
    for (int i = 0; i < inputs.Length; i++)
    {
        var dic = dictionaries[i];
    
        Console.WriteLine(i + 1);
        Console.WriteLine("\tKey\t\tValue");
        foreach (var kv in dic)
        {
            Console.WriteLine($"\tobject{kv.Key}\t\ttpe={kv.Value.type} value={kv.Value.value}");
        }
        Console.WriteLine("------------");
    }
    
    int[] keys = dictionaries.SelectMany(dic => dic.Keys).Distinct().OrderBy(_ => _).ToArray();
    int keymin = keys.First();
    
    IEnumerable<string> ie 
        = dictionaries
        .Select(dic => dic[keymin].value)
        .Concat(keys.Skip(1)
                .SelectMany(key => dictionaries
                                    .Where(dic => dic.ContainsKey(key))
                                    .Select(dic => dic[key].value)
                                    .Distinct()));
    
    string result = string.Join(";", ie);
    Console.WriteLine(result);
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful