Jump to content

Recommended Posts

Posted

I need some regex help

I inherited some data :)

The data is massive and I need a clean, fast solution :mad:

source is text and complex.  

I need to find dates such as "31-01-2018" and replace with "31-JAN-2018"

Problem is that my regex "31-01-2018" takes for ever and replaces all.

The ideal would be to search like this, but I am not managing

\d{2}-(01)-\d{4} replace (01) with JAN

But if I do it that way, the entire search string gets replaced by JAN.  This is not an error, but typically regex behaviour.  Any ideas?

Skysnake

Skysnake

Why is the snake in the sky?

Posted (edited)

@Skysnake
Another possible solution :)

#include <Date.au3>
#include <DateTimeConstants.au3>
#include <StringConstants.au3>

Global $arrDates[12] = ["31-01-2019", "01-02-2019", "02-03-2019", "03-04-2019", "04-05-2019", "05-06-2019", _
                        "06-07-2019", "07-08-2019", "08-09-2019", "09-10-2019", "10-11-2019", "11-12-2019"]

For $i = 0 To UBound($arrDates) - 1 Step 1
    ConsoleWrite("Old Date: " & $arrDates[$i] & @CRLF & _
                 "New Date: " & _MonthDigit2String($arrDates[$i]) & @CRLF & @CRLF)
Next

Func _MonthDigit2String($strDate)

    Local $arrResult = StringRegExp($strDate, "\d{2}\-(\d{2})\-\d{4}", $STR_REGEXPARRAYMATCH), _
          $strMonth = (IsArray($arrResult) ? $arrResult[0] : 0)

    If $strMonth > 0 Then Return StringRegExpReplace($strDate, "(\d{2}\-)\d{2}(\-\d{4})", "$1" & _DateToMonth($strMonth, $DMW_SHORTNAME) & "$2")

    ; Executed only if $strMonth <= 0
    Return False

EndFunc

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

This might work.

Local $str = 'I need to find dates such as "31-01-2018" and "21-02-2018"' & @CRLF & _
        ' 13-3-2018 and "1-4-2018"'

Local $aMnth[13] = [12, "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]

Local $sModifiedStr = Execute("'" & StringRegExpReplace($str, "(\d{1,2}).(\d{1,2}).(\d{2,4})", "'&$1&'-'&$aMnth[$2]&'-$3") & "'")

; Or
;Local $sModifiedStr = Execute("'" & StringRegExpReplace($str, "(\d{1,2}).(\d{1,2}).(\d{2,4})", "'&StringRight('00'&$1,2)&'-'&$aMnth[$2]&'-$3") & "'") ; One digit day (n) to two digit day (0n).

ConsoleWrite($sModifiedStr & @CRLF)

 

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
×
×
  • Create New...