Use this regexregular expression:
\w+:[\w\s]+(?![\w+:])
I tested it on: test:testvalue test2:test value test3:testvalue3
test:testvalue test2:test value test3:testvalue3
Returns 3It returns three matches:
test:testvalue
test2:test value
test3:testvalue3
You can change \w
to any character set that can occur in your input.
Code for testing this:
var regex = new Regex(@"\w+:[\w\s]+(?![\w+:])");
var test = "test:testvalue test2:test value test3:testvalue3";
foreach (Match match in regex.Matches(test))
{
var key = match.Value.Split(':')[0];
var value = match.Value.Split(':')[1];
Console.WriteLine("{0}:{1}", key, value);
}
Console.ReadLine();
EDIT: As Wonko the Sane pointed out, this regexregular expression will fail on values with :
. If you predict such situation, use \w+:[\w: ]+?(?![\w+:])
as regexthe regular expression. This will still fail when a colon in valuevalue
is preceded by space though... I'll think about solution to this.