Search code examples
c#regexdecodegs1-ai-syntaxgs1-128

EAN128 or GS1-128 decode c#


Here is the sample of EAN128 or new name GS1-128 barcode

01088888931021461712031510W1040190

I want to decode it to ais

(01)08888893102146
(17)120315
(10)W1040190

But the barcode can be changed like this

1712031510W1040190
(17)120315
(10)W1040190

is there any possible ways to solve it with regex or what is the better way

Now I am trying this

public String AICodes(String pAI)
    {
        switch (pAI)
        {
            case "01":
                return "01\\d{14}";
            case "17":
                return "17\\d{6}";
            case "10":
                return "17\\d{6}10[a-zA-Z0-9|]{1,20}";
        }
        return String.Empty;
    }

private void GS1DataConvert(string pBarcode, string pBarfnc)
    {
       Match match = Regex.Match(pBarcode, AICodes(pBarfnc));
        if (match.Success)
        {
            MessageBox.Show(match.Groups[0].Value);
        }
    }

string barfnc = "01";

GS1DataConvert(barcode, barfnc);
barfnc = "17";
GS1DataConvert(barcode, barfnc);
barfnc = "10";
GS1DataConvert(barcode, barfnc);

Solution

  • I've found RegEx to be useful still. In the following code I use a jagged string array with the AI's I want to be able to process and their properties, being:

            string[][] arrKnownAIs = new string[9][] { //AI, description, min length, max length, type, decimal point indicator? 
                                    new string[] { "00", "SSCC", "18", "18", "numeric", "false"}, 
                                    new string[] { "02", "GTIN", "14", "14", "numeric", "false"},
                                    new string[] { "10", "Batch or lot number","1", "20", "alphanumeric", "false"},
                                    new string[] { "15", "Best before date", "6", "6", "numeric", "false"},
                                    new string[] { "37", "Number of units contained", "1", "8", "numeric", "false"},
                                    new string[] { "400", "Customer's purchase order number", "1", "29", "alphanumeric", "false"},
                                    new string[] { "8005", "Price per unit of measure", "6", "6", "numeric", "false"},
                                    new string[] { "310", "Netto weight in kilograms", "7", "7", "numeric", "true"},
                                    new string[] { "315", "Netto volume in liters", "7", "7", "numeric", "true"},
                                  };
    

    I use this array to check for the AI's in the following extract of a function (with a loop cycling the array above)

    strAI = arrAI[0];
                intMin = int.Parse(arrAI[2]);
                intMax = int.Parse(arrAI[3]);
                strType = arrAI[4];
    
    strRegExMatch = "";
                if (strType == "alphanumeric")
                {
                    strRegExMatch = Regex.Match(tmpBarcode, strAI + @"\w{" + intMin + "," + intMax + "}").ToString();
                }
                else
                {
                    strRegExMatch = Regex.Match(tmpBarcode, strAI + @"\d{" + intMin + "," + intMax + "}").ToString();
                }
    
                if (strRegExMatch.Length > 0)
                {
                    tmpBarcode = Regex.Replace(tmpBarcode, strRegExMatch, ""); //remove the AI and its value so that its value can't be confused as another AI
                    strRegExMatch = Regex.Replace(strRegExMatch, strAI, ""); //remove the AI from the match
                    arrAIs[arrayIndex] = new string[] { strAI, strRegExMatch };
                }
                arrayIndex++;
    

    Hope this is helpful!