There was something wrong with position_X so I fixed it. Is it like this?
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
internal class Program
{
public class Item {
public string s_name { get; set; }
public double s_value { get; set; }
public string s_unit { get; set; }
public override string ToString() {
return $"s_name={s_name},s_value={s_value},s_unit={s_unit}";
}
}
static void Main(string[] args) {
string received
= "{s_name=length,s_value=240.000,s_unit=1.0mm};"
+ "{s_name=width,s_value=152.000,s_unit=1.0mm};"
+ "{s_name=thickness,s_value=1.600,s_unit=1.0mm};"
+ "{s_name=speed,s_value=45,s_unit=1.0mm/s};"
+ "{s_name=position_X,s_value=240.000,s_unit=1.0mm};"
+ "{s_name=position_Y,s_value=240.000,s_unit=1.0mm}";
string pattern =
@"\{s_name=(\w+),s_value=([\d.]+),s_unit=([\d.]+[a-zA-Z/]+)\}";
MatchCollection matches = Regex.Matches(received, pattern);
List<Item> items = new List<Item>();
foreach (Match match in matches) {
Item item = new Item {
s_name = match.Groups[1].Value,
s_value = double.Parse(match.Groups[2].Value),
s_unit = match.Groups[3].Value
};
items.Add(item);
Console.WriteLine($"{item}");
}
Console.ReadKey();
}
}
The rest is up to you.