emmanuel Posted October 22, 2007 Posted October 22, 2007 my goal is to change a date from this format: "10/22/2007 4:00:03 PM" to the conformist "10/22/2007 16:00:03"So, I've finally started to look into Regular Expressions, they're really cool, except when I can't get it to spit out what I want.$aHour = StringRegExp("10/22/2007 4:00:03 PM", "(?: \d{1,2}\/\d{1,2}\/\d{4}\s)(\d{1,2})", 1) If IsArray($aHour) Then For $hour In $aHour ConsoleWrite("Hour: " & $hour & @LF) Next $aMeridiem = StringRegExp($file, "(?: \d{1,2}\/\d{1,2}\/\d{4}\s\d{1,2}\:\d{1,2}\:\d{2}\s)([AM|PM]{2,2})", 1) If IsArray($aMeridiem) Then For $Meridiem In $aMeridiem ConsoleWrite("Meridiem: " & $Meridiem & @LF) Next If $Meridiem = "PM" Then $hour += 12 If $hour > 24 Then $24hour -= 24 ConsoleWrite("24 hour : " & $24hour & @LF) ConsoleWrite("24HR time: " & $lastupdated & @LF) $lastupdated = StringRegExpReplace($lastupdated, "(\s[AM|PM]{2,2})", "") ; here's the problem, the hour doesn't get replaced by the 24 hour: $lastupdated = StringRegExpReplace($lastupdated, "(\d{1-2})(?: \:\d{1-2}\:\d{2,2})", $24hour) ConsoleWrite("error: " & @error & " extended: " & @extended & @LF) ConsoleWrite("24HR time: " & $lastupdated & @LF) EndIf EndIf EndIfHere's the deal, I want to download a html file, parse the text for the time, and then, if it's more than 5 minutes old, do a refresh. I know there's probably some easier way to do it, some utility or something, but I've taken this as a lesson with RegExp.the place where I'm having an issue is with the second StringRegExpReplace, I can't seem to get it right.Thanks for any help! "I'm not even supposed to be here today!" -Dante (Hicks)
weaponx Posted October 23, 2007 Posted October 23, 2007 (edited) An alternative: #include <date.au3> MsgBox(0,"",militaryTime("10/22/2007 4:00:03 PM")) MsgBox(0,"",militaryTime("10/22/2007 14:00:03 AM")) Func militaryTime($myDateTime) Local $myDate, $myTime _DateTimeSplit ( $myDateTime, $myDate, $myTime ) Switch StringRight($myDateTime, 2) Case "AM" ;If hour is greater than 12 but is designated as AM, subtract 12 If $myTime[1] >= 12 Then $myTime[1] -= 12 EndIf Case "PM" ;If hour is less than 12 and designated as PM, add 12 If $myTime[1] <= 12 Then $myTime[1] += 12 EndIf EndSwitch Return $myDate[1] & "/" & $myDate[2] & "/" & $myDate[3] & " " & $myTime[1] & ":" & $myTime[2] & ":" & $myTime[3] EndFunc Edited October 23, 2007 by weaponx
Moderators SmOke_N Posted October 23, 2007 Moderators Posted October 23, 2007 Here... since your were trying to use RegExp... here's something a bit different than what weaponx proposed:MsgBox(0, 0, _MilitaryTime("10/22/2007 4:00:03 PM")) Func _MilitaryTime($sString) Local $aHour, $aSRE $aSRE = StringRegExp($sString, "(?i)(\d+\/\d+\/\d+)\s(\d+):(\d+:\d+)\s(AM|PM)", 3) If IsArray($aSRE) = 0 Or UBound($aSRE) - 1 <> 3 Then Return SetError(1, 0, "") If $aSRE[3] = "PM" Then If Int($aSRE[1]) = 12 Then $sHour = 12 Else $sHour = Int($aSRE[1]) + 12 EndIf Else If Int($aSRE[1]) = 12 Then $sHour = "00" Else $sHour = StringFormat("%02d", $aSRE[1]) EndIf EndIf Return StringRegExpReplace($sString,"(?i)(\d+\/\d+\/\d+)\s(\d+):(\d+:\d+)\s(AM|PM)", _ $aSRE[0] & " " & $sHour & ":" & $aSRE[2]) EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
emmanuel Posted October 23, 2007 Author Posted October 23, 2007 Excellent! I'll try those out tomorrow at work, meanwhile, I realized that there's more to do to make it match the date.au3 functions, the year needs to be at the front, like this: "2007/10/22 16:00:03" Can regexp replace do that? "I'm not even supposed to be here today!" -Dante (Hicks)
Moderators SmOke_N Posted October 23, 2007 Moderators Posted October 23, 2007 (edited) Excellent! I'll try those out tomorrow at work, meanwhile, I realized that there's more to do to make it match the date.au3 functions, the year needs to be at the front, like this: "2007/10/22 16:00:03" Can regexp replace do that?Not much you can't do with string manipulations and regular expressions. Using regular code tags to try and break the sad.gif in code:MsgBox(0, "My Way", _MilitaryTime("10/22/2007 4:00:03 PM")) MsgBox(0, "Year in front", _MilitaryTime("10/22/2007 4:00:03 PM", 1)) Func _MilitaryTime($sString, $nYearFront = 0) Local $aHour, $aSRE $aSRE = StringRegExp($sString, "(?i)(\d+\/\d+)\/(\d+)\s(\d+):(\d+:\d+)\s(AM|PM)", 3) If IsArray($aSRE) = 0 Or UBound($aSRE) - 1 <> 4 Then Return SetError(1, 0, "") If $aSRE[4] = "PM" Then If Int($aSRE[2]) = 12 Then $sHour = 12 Else $sHour = Int($aSRE[2]) + 12 EndIf Else If Int($aSRE[2]) = 12 Then $sHour = "00" Else $sHour = StringFormat("%02d", $aSRE[2]) EndIf EndIf If $nYearFront Then Return StringRegExpReplace($sString,"(?i)(\d+\/\d+)\/(\d+)\s(\d+):(\d+:\d+)\s(AM|PM)", _ $aSRE[1] & "/" & $aSRE[0] & " " & $sHour & ":" & $aSRE[3]) EndIf Return StringRegExpReplace($sString,"(?i)(\d+\/\d+\/)(\d+)\s(\d+):(\d+:\d+)\s(AM|PM)", _ $aSRE[0] & "/" & $aSRE[1] & " " & $sHour & ":" & $aSRE[3]) EndFunc Edited October 23, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now