Igor Cherkasov
Igor Cherkasov

Reputation: 107

Regex extract from string xx:xx:xx format

I am very new to programming and I have a question, I am trying to use Regex method to extract hours, minutes and seconds from a string and putting them into an array, but so far I can do it with only one number:

 int initialDay D = 0;
 string startDay = Console.ReadLine(); //input:  "It started 5 days ago"
 var resultString = Regex.Match(startDay, @"\d+").Value; 
 initialDay = Int32.Parse(resultString);   // initialDay here equals 5.

How do manage to read from a string 06: 11: 33, and transform these hours, minutes and seconds into an array of ints? So the resulting array would be like this:

int[] array = new int[] {n1, n2, n3}; // the n1 would be 6, n2 would be 11 and n3 would be 33

Thank you for your time in advance!

Upvotes: 8

Views: 1080

Answers (7)

user10216583
user10216583

Reputation:

RegEx way:

var pattern = @"(\d{1,2})\s?\:\s?(\d{1,2})\s?\:\s?(\d{1,2})";
var input = "06 : 11 : 33";
var arr = Regex.Matches(input, pattern)
    .Cast<Match>()
    .SelectMany(x => x.Groups.Cast<Group>()
    .Skip(1)
    .Select(y => int.Parse(y.Value)))
    .ToArray();

Console.WriteLine(string.Join("\n", arr));

The output:

06
11
33

regex101

Upvotes: 4

kacperfaber
kacperfaber

Reputation: 84

Instead regular expression, you can use TimeSpan.Parse() Check it https://learn.microsoft.com/pl-pl/dotnet/api/system.timespan.parse?view=netframework-4.8

Upvotes: 4

NoGTNoHappy
NoGTNoHappy

Reputation: 69

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

Upvotes: 4

Tanveer Badar
Tanveer Badar

Reputation: 5512

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.

Upvotes: 8

ElConrado
ElConrado

Reputation: 1640

If you have date as simple string you can use split method:

string dataString = "06 : 11 : 33";
string[] arrayStr = dataString.Split(':');

Then you can make int list using System.Linq:

List<int> intList = arrayStr.Select(p => Convert.ToInt32(p)).ToList();

Upvotes: 3

sticky bit
sticky bit

Reputation: 37482

You could use string.Split() to get an array of elements separated by :. Then you can loop through it, int.Parse the elements and assign them to the integer array.

string[] buffer = startDay.Split(':');
int[] array = new int[buffer.Length];
for (int i = 0; i < buffer.Length; i++)
{
    array[i] = int.Parse(buffer[i]);
}

Or you can use Linq's Select() to do the parsing.

int[] array = startDay.Split(':').Select(e => int.Parse(e)).ToArray();

Upvotes: 4

glennmark
glennmark

Reputation: 543

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.

Upvotes: 10

Related Questions