Zero0 Posted May 15, 2020 Share Posted May 15, 2020 How can I add delimiters between the characters I set, without any loop? For example I want it to look like this => ABCD-EF12-3456 $SerialStringExample1 = "ABCDEF123456" $delimiters1 = StringRegExpReplace($SerialStringExample1, '\w{4}', '-') MsgBox(0,0, $delimiters1) $SerialStringExample2 = "ABCDEF123456" $delimiters2 = StringLeft($SerialStringExample2, 4) MsgBox(0,0, $delimiters2) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 15, 2020 Moderators Share Posted May 15, 2020 Zero0, Welcome to the AutoIt forum. A simple way is to use a RegEx like this: #include <MsgBoxConstants.au3> $sSerialStringExample1 = "ABCDEF123456" $sDelimiters1 = StringRegExpReplace($sSerialStringExample1, "(.{4})(.{4})(.{4})", "$1-$2-$3") MsgBox($MB_SYSTEMMODAL, "Delimited", $sDelimiters1) No doubt a RegEx guru will be along shortly to explain why that is not the best way , but it works for me! M23 Zero0 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...
mikell Posted May 15, 2020 Share Posted May 15, 2020 $SerialStringExample1 = "ABCDEF123456" $delimiters1 = StringRegExpReplace($SerialStringExample1, '\w{4}\K(?!$)', '-') MsgBox(0,0, $delimiters1) FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 15, 2020 Moderators Share Posted May 15, 2020 See, I just knew it would happen! M23 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...
mikell Posted May 15, 2020 Share Posted May 15, 2020 (edited) Well I must admit that your regex is more reliable, because it may return an error if let's say the string is 11 chars long only... Edited May 15, 2020 by mikell Zero0 1 Link to comment Share on other sites More sharing options...
Zero0 Posted May 15, 2020 Author Share Posted May 15, 2020 7 minutes ago, mikell said: $SerialStringExample1 = "ABCDEF123456" $delimiters1 = StringRegExpReplace($SerialStringExample1, '\w{4}\K(?!$)', '-') MsgBox(0,0, $delimiters1) @mikell Great sample code, Thank you. Are there any other examples without the loop? Except StringRegExpReplace. Link to comment Share on other sites More sharing options...
mikell Posted May 15, 2020 Share Posted May 15, 2020 Oh yes... you can use some kind of pseudo-loop #include <MsgBoxConstants.au3> $sSerialStringExample1 = "ABCDEF12345" $sDelimiters1 = StringLeft($sSerialStringExample1, 4) & "-" & StringMid($sSerialStringExample1, 5, 4) & "-" & StringRight($sSerialStringExample1, 4) MsgBox($MB_SYSTEMMODAL, "Delimited", $sDelimiters1) Zero0 1 Link to comment Share on other sites More sharing options...
SirAlonne Posted May 15, 2020 Share Posted May 15, 2020 Global $Regex, $String = "ABCDEF123456" Global $Pattern = "^(\w{4})(\w{4})(\w{4})$" ; ^ <-- beginning of the string ... end of string --> $ For $x = 1 To 2 Step 1 If $x = 2 Then $String &= "5_" EndIf If StringRegExp($String, $Pattern) Then $Regex = StringRegExpReplace($String, $Pattern, "$1" & "-" & "$2" & "-" & "$3") MsgBox(0, "Result if string is in the defined format", "String =" & $String & _ @CRLF & "Result =" & $Regex) Else MsgBox(0, "Result if not", $String & " does not match.") EndIf Next Zero0 1 Link to comment Share on other sites More sharing options...
mikell Posted May 16, 2020 Share Posted May 16, 2020 No need to run it twice to get a valid error checking $String = "ABCDEF123456" $new = StringRegExpReplace($String, "^(\w{4})(\w{4})(\w{4})$", "$1-$2-$3") MsgBox(0, @extended ? "ok" : "error", @extended ? $new : "error" ) Zero0 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 16, 2020 Share Posted May 16, 2020 Let's wrap it into a function: #include <StringConstants.au3> ConsoleWrite(_StringDelimiter("ABCDEFGH123456", 2) & @CRLF) Func _StringDelimiter($strString, $intNumber) Return Mod(StringLen($strString), $intNumber) = 0 ? StringRegExpReplace($strString, '\w{' & $intNumber & '}\K(?!$)', '-') : '' EndFunc Zero0 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
SirAlonne Posted May 16, 2020 Share Posted May 16, 2020 My point was the definition of the beginning ^ and end $ of the string, without that error cheking is not working. Anyhow @mikell, nice use of conditional operators there! I did not know that it works inside a message box. Link to comment Share on other sites More sharing options...
Gianni Posted May 16, 2020 Share Posted May 16, 2020 (edited) 22 hours ago, Zero0 said: Are there any other examples without the loop? Except StringRegExpReplace. ... through recursion MsgBox(0, '', _stringsInsertDelims('ABCDEF123456', 4)) ; inserts delimiters after each group of nr. characters and returns the resulting string Func _stringsInsertDelims($sString, $iGroupsLen = 1, $sDelim = '-') If Number($iGroupsLen) < 1 Then Return SetError(1, 0, $sString) Local $sSnippt = StringLeft($sString, $iGroupsLen) Local $sReminder = StringTrimLeft($sString, $iGroupsLen) If $sReminder <> '' Then $sSnippt &= $sDelim & _stringsInsertDelims($sReminder, $iGroupsLen, $sDelim) Return $sSnippt EndFunc ;==>_stringsInsertDelims Edited May 16, 2020 by Chimp added 3th parameter in recursion call Zero0 and mikell 2 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... 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