1

I have simple text:

first_name=value1
secon_name=value2
date_b = 12.01.1989

Rows is separated by \n char. I have code which split this string and then I iterate through array and check the keys:

string[] data = str.Split('\n');
foreach (var row in data)
{
   if (row.StartsWith("first_name"))
   {
       obj.FirstName = row.Remove(0, ("first_name").Length);
       ...
   }
}

But there are about 15 pairs and the code in foreach very unreadable. How to parse this with regular expression? I want to get dictionary<key, value>.

PS. Some rules:
1. The key is without whitespace.
2. The value can contain whitespace.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
user348173
  • 8,818
  • 18
  • 66
  • 102

1 Answers1

4
(.*?)\s*=\s*([^\s]+)

This should work.Will give you groups containing both the matches.

Have a look.

http://regex101.com/r/wE3dU7/4

vks
  • 67,027
  • 10
  • 91
  • 124