Skysnake Posted February 28, 2019 Posted February 28, 2019 I need some regex help I inherited some data The data is massive and I need a clean, fast solution 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?
user4157124 Posted February 28, 2019 Posted February 28, 2019 Using StringRegExpReplace() : (\d{2})-(01)-(\d{4}) Replace by: $1-JAN-$3 AUERLO (AutoIt error logger)
Skysnake Posted February 28, 2019 Author Posted February 28, 2019 Thx. Checking Skysnake Why is the snake in the sky?
FrancescoDiMuro Posted February 28, 2019 Posted February 28, 2019 (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 February 28, 2019 by FrancescoDiMuro Skysnake 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Skysnake Posted February 28, 2019 Author Posted February 28, 2019 (edited) 1 hour ago, FrancescoDiMuro said: Another possible solution You're a beast And I mean that in a nice way Thx Edited February 28, 2019 by Skysnake FrancescoDiMuro 1 Skysnake Why is the snake in the sky?
FrancescoDiMuro Posted February 28, 2019 Posted February 28, 2019 13 minutes ago, Skysnake said: And I mean that it a nice way Never thought the opposite Thank you Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Malkey Posted February 28, 2019 Posted February 28, 2019 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) FrancescoDiMuro, iamtheky and Skysnake 3
FrancescoDiMuro Posted February 28, 2019 Posted February 28, 2019 @Malkey Nice one! Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
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