[dba-VB] C# Regex?

Gustav Brock Gustav at cactus.dk
Thu Mar 29 03:19:32 CDT 2012


Hi Michael

You could cheat a little and use a reverse split and timespan:

<C#>
// string t = "1 hr  12 min 26 sec";
// string t = " 12 min 26 sec";
string t = " 26 sec";
string time = "0:0:" + t.TrimStart().Replace("hr", ":").Replace("min", ":").Replace("sec", "").Replace(" ", "");
Char[] separators = {':'};
IEnumerable<string> timeParts= time.Split(separators).Reverse();
TimeSpan timeSpan = new TimeSpan(int.Parse(timeParts.ElementAt(2)), int.Parse(timeParts.ElementAt(1)), int.Parse(timeParts.ElementAt(0)));
int seconds = Convert.ToInt32(timeSpan.TotalSeconds);
Console.WriteLine(time);
Console.WriteLine(seconds.ToString());
Console.ReadKey();
</C#>

/gustav


>>> michael at ddisolutions.com.au 29-03-2012 07:04 >>>
Hi Guys,

Anyone good at regex?

My strings could look like any of these
	1 sec 
	11 sec
	1 min 5 sec
	1 min 26 sec
	1 hr  1 min 5 sec
	1 hr  12 min 26 sec	

The goal is to get total # of seconds but splitting into a
MatchCollection would do.

Any takers?

Cheers

Michael M





More information about the dba-VB mailing list