Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx to Match Stupid Time Format and Convert to Something Better 1

Status
Not open for further replies.

strebor

Technical User
Nov 24, 2004
66
0
0
US
I have text files that contain time in a strange format which needs to be converted.

The format is:
3 days 14 hours 34 minutes 17 seconds

I want to convert it to 86:34:17 (which will then be able to be manipulated by Excel, etc.)

I have a regEx pattern:
"(\d+\sdays)?(\s?\d+\shours)?(\s?[0-5]?\d\sminutes)?(\s)?[0-5]?\d\sseconds"

The general flow is to read a line of the text from a file, find the pattern that matches, and pass that matched value as a string to a function that converts that time to the simple format and uses it as the replacement value.

This was straightforward in VBScript...

Code:
	Set re = New RegExp
	re.Global = True
	myPattern = "(\d+\sdays)?(\s?\d+\shours)?(\s?[0-5]?\d\sminutes)?(\s)?[0-5]?\d\sseconds"
	
	
	Do While Not objTextFile.AtEndOfStream
		s = objTextFile.ReadLine
		lineOfText = s
		re.Pattern = myPattern
		Set objMatch = re.Execute(s)
		For Each match In objMatch
			lineOfText = re.Replace(s, ConvertToHoursMinsSeconds(match.value))
		Next

		objCSVFile.WriteLine lineOfText
		


	Loop

In Visual Basic I am not understanding how I can get the value of the match and pass it to a function in a similar way?

strebor
 

Would it be better if you would have your data in the format of:
[tt]
Element(0) = 3
Element(1) = days
Element(2) = 14
Element(3) = hours
Element(4) = 34
Element(5) = minutes
Element(6) = 17
Element(7) = seconds
[/tt]
Just Split it by the space, and write a simple Select Case, detect Days, hours, etc, and take a value from (element -1)

Have fun.

---- Andy
 
Thank you jges!

The link was very helpful... here is what works.

Code:
Private Sub mainProcess()
        Dim stringOut As String

        Dim fileReader As System.IO.StreamReader
        Dim testFile As System.IO.FileInfo
        Try
            fileReader = My.Computer.FileSystem.OpenTextFileReader(txtSourceFile.Text)
            testFile = My.Computer.FileSystem.GetFileInfo(txtSourceFile.Text)
        Catch ex As Exception
            MessageBox.Show("The source file appears to be open. Close it and try again")
        End Try

        Dim stringReader As String


        Dim fileWriter As StreamWriter
        Try
            fileWriter = File.CreateText(txtSaveFolder.Text & "\" & txtCaseNbr.Text & "_" & testFile.Name)
        


            Do While Not fileReader.EndOfStream
                stringReader = fileReader.ReadLine()
                stringOut = MRST(stringReader)
                fileWriter.WriteLine(stringOut)
            Loop
            fileReader.Close()
            fileWriter.Close()

            MessageBox.Show("All Done")
        Catch exc As Exception
            MessageBox.Show("The file: " & txtSaveFolder.Text & "\" & txtCaseNbr.Text & ".csv already exists. Choose a different file name.")
        
        End Try

    End Sub


    Function MRST(ByRef value As String) As String

        Return Regex.Replace(value, "(\d+\sdays)?(\s?\d+\shours)?(\s?[0-5]?\d\sminutes)?(\s)?[0-5]?\d\sseconds", _
                             New MatchEvaluator(AddressOf ConvertToHoursMinsSeconds))
    End Function

The MRST function just converts the found string into the simple time format.



strebor
 
ooops... I should say: the ConvertToHoursMinsSeconds function converts the found string to the simple time.

strebor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top