Thomas Adler

Thomas Adler

  • NA
  • 29
  • 359

RegEx ; GroupNames ; fill in the dictionary.

Mar 13 2024 7:44 PM

Hello experts, I need your help.

I have this input and want to fill a dictionary.
The group names can be more or less.

My solution works, but is not well solved.
How can I make it better, which would be the question.
Thank you.

//text = "[)>@06@PTVK1281257@21P@3SID-214214@@";     should also possible empty string 21P is empty, no value however.

My trials
//[)>@06@PTVK1281257@2P@14D15072024@ZLevel34@KReceiver@16KEN-en@VRuvel@3SID-214214@Q456NAR@1ZGermanCreate@@


//P     TVK1281257
//2P
//14D   15072024
//Z     Level34
//K     Receiver
//16K   EN-en
//V     Ruvel
//3S    ID-214214
//Q     456NAR  or only 456
//1Z    GermanCreate

string regEx = @"1Z(?<BatchId>([A-Za-z0-9/_,#\:\%\-\*\+\.]{1,17}))|3S(?<IDCustom>([A-Za-z0-90-9]{9}))|P(?<ProductPartNumber>([A-Z0-9]{10}))|2P(?<SupplierPartNumber>([A-Z0-9]{20}))|14D(?<ManufactorDate>([0-9]{7}))|Z(?<MSLClass>([A-Za-z0-90-9]{2,22}))|K(?<ArticleMaster>([A-Za-z0-90-9]{3}))|16K(?<UnitCode>([A-Za-z0-90-9]{5}))|V(?<Vendor>([A-Za-z0-90-9]{5}))|Q(?<Quantity>([0-9]{3}))";

text = "[)>@06@PTVK1281257@2P66@14D15072024@ZLevel34@KReceiver@16KEN-en@VRuvel@3SID-214214@Q456NAR@1ZGermanCreate@@";
//text = "[)>@06@PTVK1281257@21P66777@3SID-214214@@";
//text = "[)>@06@PTVK1281257@21P@3SID-214214@@";     should also possible empty string


//Dictionary<string, string> results =
//Regex.Matches(text, regEx).Cast<Match>().ToDictionary(ma => ma.Groups[1].Value, ma => ma.Groups[2].Value);


Dictionary<string, string> results = new Dictionary<string, string>();
Regex rgx = new Regex(regEx);
Match match = rgx.Match(text);
while (match.Success)
{
	string[] names = rgx.GetGroupNames();
	Console.WriteLine("Named Groups:");
	foreach (var name in names)
	{
		Group grp = match.Groups[name];
		Console.WriteLine("   {0}: '{1}'", name, grp.Value);
	}


	if (match.Groups["BatchId"].Name == "BatchId" && match.Groups["BatchId"].Value != "")
		results.Add("BatchId", match.Groups["BatchId"].Value);
	else if (match.Groups["IDCustom"].Name == "IDCustom" && match.Groups["IDCustom"].Value != "")
		results.Add("IDCustom", match.Groups["IDCustom"].Value);
	else if (match.Groups["ProductPartNumber"].Name == "ProductPartNumber" && match.Groups["ProductPartNumber"].Value != "") 
		results.Add("ProductPartNumber", match.Groups["ProductPartNumber"].Value);
	else if (match.Groups["SupplierPartNumber"].Name == "SupplierPartNumber" && match.Groups["SupplierPartNumber"].Value != "") 
		results.Add("SupplierPartNumber", match.Groups["SupplierPartNumber"].Value);
	else if (match.Groups["ManufactorDate"].Name == "ManufactorDate" && match.Groups["ManufactorDate"].Value != "") 
		results.Add("ManufactorDate", match.Groups["ManufactorDate"].Value);
	else if(match.Groups["MSLClass"].Name == "MSLClass" && match.Groups["MSLClass"].Value != "")  
		results.Add("MSLClass", match.Groups["MSLClass"].Value);

	else if (match.Groups["ArticleMaster"].Name == "ArticleMaster" && match.Groups["ArticleMaster"].Value != "") 
		results.Add("ArticleMaster", match.Groups["ArticleMaster"].Value);
	else if (match.Groups["UnitCode"].Name == "UnitCode" && match.Groups["UnitCode"].Value != "") 
		results.Add("UnitCode", match.Groups["UnitCode"].Value);
	else if(match.Groups["Vendor"].Name == "Vendor" && match.Groups["Vendor"].Value != "")  
		results.Add("Vendor", match.Groups["Vendor"].Value);
	else if (match.Groups["Quantity"].Name == "Quantity" && match.Groups["Quantity"].Value != "") 
		results.Add("Quantity", match.Groups["Quantity"].Value);

	match = match.NextMatch();
}

Thanks for your help!


Answers (7)