JohnOne

StringRegExpReplace (date format)

6 posts in this topic

#1 ·  Posted (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 by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



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! :D

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
1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

$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)

1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

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
1 person likes this

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now