Nasir
Nasir

Reputation: 77

Split string and numbers

I am trying to split this string " "109491: Navy/Red115138: Navy/Light Grey" in Colour Code and colour name. The numeric is colour-code and the string including \ is color name. I have tried this regular expression "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)" but it did not work as desired . Some times it is returning empty in Colour Code.

Thanks

Upvotes: 1

Views: 137

Answers (2)

Lloyd
Lloyd

Reputation: 29668

Try something like this:

(?:(\d+):\s([^\d]+))+?

This will capture the numbers and text as separate captures.

For example:

enter image description here

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65087

Simply use this:

(\d+[^\d]+)

It is "select numbers.. then anything else that isn't a number". So it matches like this:

109491: Navy/Red115138: Navy/Light Grey
|______________||______________________|

E.g:

var str = "109491: Navy/Red115138: Navy/Light Grey";
var matches = new List<string>();

foreach (Match match in Regex.Matches(str, @"(\d+[^\d]+)")) {
    matches.Add(match.Value);
}

// matches[0] = 109491: Navy/Red
// matches[1] = 115138: Navy/Light Grey

Upvotes: 1

Related Questions