Почему не простой парсер? Разделите на символ '+'
, затем оцените каждую фразу. Я предполагаю, что первое слово перед пробелом является ключом, а остаток - значением. Есть также регулярное выражение, которое проверяет правильность символов; не буквенно-цифровой вызовет исключение.
var working = "name test +company abc def +phone 3434 +vehicle test + interested yyy +invited zzz";
if (System.Text.RegularExpressions.Regex.IsMatch(working, "[^a-zA-Z0-9 +]"))
{
throw new InvalidOperationException();
}
var values = working.Split('+').Select(x => x?.Trim() ?? string.Empty);
foreach (var phrase in values)
{
string left, right;
var space = phrase.IndexOf(' ');
if (space > 0)
{
left = phrase.Substring(0, space)?.Trim() ?? string.Empty;
right = phrase.Substring(space + 1, phrase.Length - space - 1)?.Trim() ?? string.Empty;
Console.WriteLine("left: [" + left + "], right: [" + right + "]");
}
}
Выход на консоль:
left: [name], right: [test]
left: [company], right: [abc def]
left: [phone], right: [3434]
left: [vehicle], right: [test]
left: [interested], right: [yyy]
left: [invited], right: [zzz]
Запуск вышеуказанного с недопустимым символом вызывает исключение:
var working = "na%me test +company abc def +phone 3434 +vehicle test + interested yyy +invited zzz";
...
Operation is not valid due to the current state of the object.