RegEx Procedure to find and define the RegEx.

Markus Freitag 3,791 Reputation points
2024-03-08T17:55:14.02+00:00

Hello,

I am looking for a solution how to create a regex. Are there any tools? Are there AI tools? I got this from Gego @gekka , works great. When I am given a new task, how do I find a solution, what can I do? Reason for this request.

Sample: What's happen when we get this?
Object0-EAN13CODE^PRODUCT=730180^Object1-TEXT^104612^Object2-TEXT^27730180^Object3-TEXT^AT1
Object0-EAN13CODE^PRODUCT=730181^Object1-TEXT^104612^Object2-TEXT^27730181^Object3-TEXT^AT1
// and I need to split it additional.
// OR
Object0-EAN13CODE^PROD730180^Object1-TEXT^104612^Object2-TEXT^27730180^Object3-TEXT^AT1
Object0-EAN13CODE^PROD730181^Object1-TEXT^104612^Object2-TEXT^27730181^Object3-TEXT^AT1
// and I have a prefix like this PROD
Input 
   1 to n entries like that
   
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
Object0-EAN13CODE^27730183^Object1-TEXT^104612^Object2-TEXT^27730183^Object3-TEXT^AT1
   
RegEx Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^
RegEx  (?<=(^|\^))Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^
       (?<=(^|\^))Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^
	   (?<=^|\^)Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^

How do I go about finding a solution?

 //  (?<=(^|\^))  what is it? Works too, without it.
 //  \d+ means match one or more digits.
 //  .+? form requires at least one character to match, while .*? can match none at all
 //  .*? matches any character (.) any number of times (*), as few times as possible to make the regex match (?).

How can I read out the group names and values specifically? See the picture.

enter image description here

 1
 	Key		Value
 	object0		type=DATAMATRIX value=27730180
 	object1		type=TEXT value=123419
 	object2		type=TEXT value=27730180
 	object3		type=TEXT value=AT1
 ------------
 2
 	Key		Value
 	object0		type=DATAMATRIX value=27730181
 	object1		type=TEXT value=123419
 	object2		type=TEXT value=27730181
 	object3		type=TEXT value=AT1
 ------------

Testtool

Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2024-03-15T12:00:25.6033333+00:00

    Hi @Markus Freitag , Welcome to Microsoft Q&A,

    The (?<=(^|\^)) inside the regex pattern is a positive lookbehind assertion. It asserts that what precedes the current position in the string is either the start of the string ^ or a ^ character.

    Now I would like to have a dictionary that contains the group names

    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^",
    };
    
    // Define a simpler regex pattern
    Regex reg = new Regex(@"Object(?<NUM>\d+)-(?<KEY>.+?)\^(?<VALUE>.*?)\^");
    
    var dictionaries = new List<Dictionary<int, object>>();
    
    foreach (string input in inputs)
    {
        // Match all key-value pairs in the input string
        MatchCollection matches = reg.Matches(input);
    
        // Create a dictionary to hold the key-value pairs for this input
        Dictionary<int, object> dict = new Dictionary<int, object>();
    
        // Iterate over each match and add it to the dictionary
        foreach (Match match in matches)
        {
            int num = int.Parse(match.Groups["NUM"].Value);
            string key = match.Groups["KEY"].Value;
            string value = match.Groups["VALUE"].Value;
    
            // Store key-value pairs in the dictionary
            dict[num] = new { type = key, value = value };
        }
    
        // Add the dictionary for this input to the list of dictionaries
        dictionaries.Add(dict);
    }
    
    // Output the dictionaries
    for (int i = 0; i < dictionaries.Count; i++)
    {
        Console.WriteLine($"Dictionary {i + 1}:");
        foreach (var kvp in dictionaries[i])
        {
            Console.WriteLine($"  Key: Object{kvp.Key}, Value: type={kvp.Value.GetType().GetProperty("type").GetValue(kvp.Value)}, value={kvp.Value.GetType().GetProperty("value").GetValue(kvp.Value)}");
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.