JohnOne Posted February 23, 2015 Share Posted February 23, 2015 (edited) I'm trying to change some date formats to simple numerical values for easy sorting. With the assistance of the help file I managed to complete the first format, but the second one, I am stuck at. $Date = '21/09/2009 21:35' $DatePattern = '(\d{2})/(\d{2})/(\d{4})( )(\d{2})(:)(\d{2})' $SRER = StringRegExpReplace($Date, $DatePattern, '$3$2$1$5$7') ConsoleWrite($Date & @LF & @LF & $SRER & @LF & @LF & @error & " " & @extended & @LF) $Date2 = '16-Mar-2010 22:14:00' $DatePattern2 = "^(\d\d)-([A-Za-z]{3})-(\d{4})( )(\d{2})(:)(\d{2})(:)(\d{2})\z" $SRER2 = StringRegExpReplace($Date2, $DatePattern2, '$3$2$1$5$7') ConsoleWrite($Date2 & @LF & @LF & $SRER2 & @LF & @LF & @error & " " & @extended & @LF) In this case, "Mar" should be replaced with "03". If it were "Dec" it would be replaced with "12". I was shown a stricter way to capture that part by another member SadBunny in another thread, but I still do not know how I could replace with corresponding numerical value. "^([0-3]?[0-9]-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9])\z" Any help and explanations are warmly welcomed. Edited February 23, 2015 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 23, 2015 Moderators Share Posted February 23, 2015 JohnOne,Trying to deal with date formats (particularly ones with letter months/days) is very difficult and I am not sure you can do it in a single RegEx - cue for someone to prove me wrong! I had to write quite a complex UDF to do it - look in my sig for Date_Time_Convert: #include "DTC.au3" $sDate = '21/09/2009 21:35' $sRet = _Date_Time_Convert($sDate, "dd/MM/yyyy HH:mm", "yyyyMMddHHmm") ConsoleWrite($sRet & @CRLF) $sDate = '16-Mar-2010 22:14:00' $sRet = _Date_Time_Convert($sDate, "dd-MMM-yyyy HH:mm:ss", "yyyyMMddHHmm") ConsoleWrite($sRet & @CRLF)M23 JohnOne 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JohnOne Posted February 23, 2015 Author Share Posted February 23, 2015 Thanks M That works a treat. Appreciate it. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
iamtheky Posted February 23, 2015 Share Posted February 23, 2015 $Date2 = '16-Mar-2010 22:14:00' $DatePattern2 = "^(\d\d)-([A-Za-z]{3})-(\d{4})( )(\d{2})(:)(\d{2})(:)(\d{2})\z" $SRER2 = StringRegExpReplace($Date2, $DatePattern2, '$3$2$1$5$7') Dim $MonthAbb[13] = [12 , "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"] For $i = 1 to 12 $SRER2 = stringreplace($SRER2 , $MonthAbb[$i] , stringformat("%02s" , $i)) Next ConsoleWrite($Date2 & @LF & @LF & $SRER2 & @LF & @LF & @error & " " & @extended & @LF) JohnOne 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
SadBunny Posted February 23, 2015 Share Posted February 23, 2015 (edited) $Date2 = '16-Mar-2010 22:14:00' $DatePattern2 = "^(\d\d)-([A-Za-z]{3})-(\d{4})( )(\d{2})(:)(\d{2})(:)(\d{2})\z" $SRER2 = StringRegExpReplace($Date2, $DatePattern2, '$3$2$1$5$7') Dim $MonthAbb[13] = [12 , "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"] For $i = 1 to 12 $SRER2 = stringreplace($SRER2 , $MonthAbb[$i] , stringformat("%02s" , $i)) Next ConsoleWrite($Date2 & @LF & @LF & $SRER2 & @LF & @LF & @error & " " & @extended & @LF) At the very least case-insensitivize the patterns Yes, and then someone runs this on a Dutch or German system and Mar, May and Oct suddenly become month 0 or are not replaced and your whole system breaks in three different months of the year. I hate dateformats with alpha chars in them Especially stupid are dates retrieved from a MSSQL server. If you duplicate a MSSQL server including DB's in them but install the thing on a Windows Server OS in another language, the dates come in another language by default. Fun, if your dev env has a dutch server and your prod env has an English one, because of cheap idiots managing idiotic M$ licensing. Grmbl. Sorry for the rant. Also, even if it would be possible with a single regex (and it probably is), I couldn't be arsed in the slightest This is one of these cases where I wonld only use regex to match stuff, then use a lookup array to fix like boththose. Edited February 23, 2015 by SadBunny Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
Malkey Posted February 23, 2015 Share Posted February 23, 2015 Here is an English version. Local $Date = '16-Mar-2010 22:14:00' Local $Date2 = Execute(StringRegExpReplace($Date, "(?i)^(\d{2})-([A-Z]{3})-(\d{4})\h(\d{2}):(\d{2}):(\d{2})$", _ "'$3' & StringRight('0' & UBound(StringRegExp('$2', '(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)|', 3)) - 1, 2) & '$1$4$5$6'")) ConsoleWrite($Date2 & @LF) ; Returns 20100316221400 JohnOne 1 Link to comment Share on other sites More sharing options...
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