[dba-VB] C# Regex?

Michael Maddison michael at ddisolutions.com.au
Thu Mar 29 17:28:14 CDT 2012


Nice, never occurred to use TimeSpan.
I'll try it out.

Cheers

Michael M

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Thursday, 29 March 2012 7:20 PM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] C# Regex?

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



_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1424 / Virus Database: 2113/4903 - Release Date: 03/29/12



More information about the dba-VB mailing list