Skip to main content
The 2024 Developer Survey results are live! See the results
Removed historical information.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

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.

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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 regex will fail on values with :. If you predict such situation use \w+:[\w: ]+?(?![\w+:]) as regex expression. This will still fail when colon in value is preceded by space though... I'll think about solution to this.

Use this regular expression:

\w+:[\w\s]+(?![\w+:])

I tested it on

test:testvalue test2:test value test3:testvalue3

It 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();

As Wonko the Sane pointed out, this regular expression will fail on values with :. If you predict such situation, use \w+:[\w: ]+?(?![\w+:]) as the regular expression. This will still fail when a colon in value is preceded by space though... I'll think about solution to this.

Added regex for values with colons.; added 1 characters in body
Source Link
Episodex
  • 4.5k
  • 4
  • 42
  • 62

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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 regex will fail on values with :. If you predict such situation use \w+:[\w: ]+?(?![\w+:]) as regex expression. This will still fail when colon in value is preceded by space though... I'll think about solution to this.

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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();

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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 regex will fail on values with :. If you predict such situation use \w+:[\w: ]+?(?![\w+:]) as regex expression. This will still fail when colon in value is preceded by space though... I'll think about solution to this.

Added example code
Source Link
Episodex
  • 4.5k
  • 4
  • 42
  • 62

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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();

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 matches:

test:testvalue
test2:test value
test3:testvalue3

You can change \w to any character set that can occur in your input.

Use this regex:

\w+:[\w\s]+(?![\w+:])

I tested it on: test:testvalue test2:test value test3:testvalue3

Returns 3 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();
Source Link
Episodex
  • 4.5k
  • 4
  • 42
  • 62
Loading