Share via

Regex for QRCode Information

Kmcnet 1,376 Reputation points
2024-12-23T19:58:28.1133333+00:00

Hello everyone and thanks for the help in advance. I am developing a program to read information scanned by a QRCode reader. Here is a sample of the input

(01)00358160740213(17)260515(10)32PF3(21)5HZ6R1PX8C

The input includes the GS1 separator fields in parenthesis. These fields may come in a different order or may include other separator characters. The obvious brute force method would be to split using the ( character and go from there, but I was wondering if there was amore elegant and extensible way of accomplishing this. Any help would be appreciated.

Developer technologies | C#
Developer technologies | 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.


Answer accepted by question author

Anonymous
2024-12-24T02:12:11.6566667+00:00

Hi @Kmcnet , Welcome to Microsoft Q&A,

If you mean using regular expressions to split, you can use it like this:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace xxx
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = "(01)00358160740213(17)260515(10)32PF3(21)5HZ6R1PX8C";

            // Define the regular expression pattern to match the GS1 separator and its value
            string pattern = @"\((\d{2})\)([^\(]+)";
            var matches = Regex.Matches(input, pattern);

            // Create a dictionary to store the parsing results
            var parsedData = new Dictionary<string, string>();

            foreach (Match match in matches)
            {
                // Group 1 is the separator, group 2 is the corresponding value
                string key = match.Groups[1].Value;
                string value = match.Groups[2].Value;

                parsedData[key] = value;
            }

            Console.WriteLine("Analysis results:");
            foreach (var item in parsedData)
            {
                Console.WriteLine($"Delimiter: {item.Key}, Value: {item.Value}");
            }

            Console.ReadLine();
        }
    }
}

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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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