[dba-VB] C# Regex?

Michael Maddison michael at ddisolutions.com.au
Thu Mar 29 22:41:21 CDT 2012


Adam,

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.


        private int GetRecLength ( string value )
        {
            //x hr 3 min 8 sec
            string hr = string.Empty, min = string.Empty, sec =
string.Empty;
            int val = 0;
            //sec = value.Substring ( value.Length - 6, 2 );
            //min = value.Substring(value.Length - 


            //@"(^[0-9]*$)"  min\s(?<sec>\d\S+)

            Regex r = new Regex ( @"[0-9]+" );
            MatchCollection mc = r.Matches ( value );
            
            
            min = mc [ 0 ].Value;
            sec = mc [ 1 ].Value;
	
           //Change to looping through collection members
            if ( !string.IsNullOrEmpty ( min ) ) val = Convert.ToInt32 (
min ) * 60;
            if ( !string.IsNullOrEmpty ( sec ) ) val = Convert.ToInt32 (
sec ) + val;
            // 
            //(?<min>\d\S+\s)min\s(?<sec>\d\S+)

            return val;
        }


Cheers

Michael M



More information about the dba-VB mailing list