In regex patterns, + indicates One or More.
Try using this string instead
@"^(?<Key>\w+)\s+(?<Value>.*)$
The * indicates Any Number including 0.
Update
I tested the following code, and got this output.
string t1 = "field1 ";
string t2 = "field2 iopoi";
string t3 = "field3 12.12.12";
Regex rTest = new Regex(@"^(?<Key>\w+)\s+(?<Value>.*)$");
if (rTest.IsMatch(t1))
{
MessageBox.Show("T1 match");
foreach (Match m in rTest.Matches(t1))
textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
}
textBox1.Text += "\n\n";
if (rTest.IsMatch(t2))
{
MessageBox.Show("T2 match");
foreach (Match m in rTest.Matches(t2))
textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
}
textBox1.Text += "\n\n";
if (rTest.IsMatch(t3))
{
MessageBox.Show("T3 match");
foreach (Match m in rTest.Matches(t3))
textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
}
Output:
Key: field1 Value:
Key: field2 Value: iopoi
Key: field3 Value: 12.12.12
I also tested this code calling .Trim() on each initial string.
t1 DID NOT MATCH after calling .Trim()
The reason for this, is because .Trim removes all the whitespace following field1, or field33, or whatever, and the regex requires One or More whitespace characters.
New Regex: Try using this instead @"^(?<Key>\w+)\s*(?<Value>.*)$"
Note that now the \s is also followed by a *. Now it should match after using Trim as well.