Regex extract from string xx:xx:xx format

If the input is in this format (dd:dd:dd), you actually don't need regex in this. You can use String.Split() method. For example:

string test = "23:22:21";
string []res = test.Split(':');

The res array will now contains "23", "22", "21" as its elements. You just then need to convert them into int.


Unless you are trying to learn regular expressions, there is no reason for you to perform this parsing yourself.

Use TimeSpan.Parse() method for this task.


Use Regex.Matches(string input, string pattern) like this:

var results = Regex.Matches(startDay, @"\d+");
var array = (from Match match in results
             select Convert.ToInt32(match.Value))
            .ToArray();

Tags:

C#

Regex