phatzilla Posted November 10, 2014 Share Posted November 10, 2014 (edited) I have a string "abc123def432ghi678". I'd like to replace each digit in this string with another DIFFERENT digit. Here's what i have so far $string="abc123def432ghi678" if StringRegExp($string,"[0-9]") Then $newstring = StringRegExpReplace($string,"[0-9]",random(0,9,1)) EndIf msgbox(4096,'',$newstring) The problem with this is two fold 1) it replaces each digit with ONE digit that it chooses, so for example, the newstring will turn out to be: abc111def111ghi111 or abc555def555ghi555 etc etc 2) There's no guarentee that it wont replace the digit with a different one, since its just using a random function. How would i go about replacing each digit in the given string, with a different new digit? Edited November 10, 2014 by phatzilla Link to comment Share on other sites More sharing options...
orbs Posted November 10, 2014 Share Posted November 10, 2014 (edited) the reason it's only a specific digit that is placed instead of the original digits, is that the Random() expression is evaluated only once. and besides, what is the If good for? if no digits found, nothing would be replaced anyway. i'd assume a RegExp solution exists, but i'd go about it the simple way: $string="abc123def432ghi678" $newstring=ReplaceDigits($string) msgbox(4096,'',$newstring) Func ReplaceDigits($sInput) Local $sOutput='', $i For $i=1 To StringLen($sInput) If StringIsInt(StringMid($sInput,$i,1)) Then $sOutput&=Random(0,9,1) Else $sOutput&=StringMid($sInput,$i,1) EndIf Next Return $sOutput EndFunc and as for your second requirement: $string="abc123def432ghi678" $newstring=ReplaceDigits($string) msgbox(4096,'',$newstring) Func ReplaceDigits($sInput) Local $sOutput='', $i, $sNewDigit For $i=1 To StringLen($sInput) If StringIsInt(StringMid($sInput,$i,1)) Then Do $sNewDigit=Random(0,9,1) Until $sNewDigit<>StringMid($sInput,$i,1) $sOutput&=$sNewDigit Else $sOutput&=StringMid($sInput,$i,1) EndIf Next Return $sOutput EndFunc and just for our curiosity, what is this for? Edited November 10, 2014 by orbs Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
jguinch Posted November 10, 2014 Share Posted November 10, 2014 $string="abc123d1ef432ghi678" $string = Execute ( StringRegExpReplace("'" & $string & "'" , "\d", "' & Random(0, 9, 1) & '") ) ConsoleWrite($string) From jchd Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Malkey Posted November 10, 2014 Share Posted November 10, 2014 Another example. Local $string = "abc123d1ef432ghi12345678901234567890" ConsoleWrite($string & @LF) $string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _Random(\1) & '")) ConsoleWrite($string & @LF) Func _Random($iVal) Local $iRet = Random(0, 9, 1) While $iRet = $iVal $iRet = Random(0, 9, 1) WEnd Return $iRet EndFunc ;==>_Random #cs An example return:- abc123d1ef432ghi12345678901234567890 abc318d9ef606ghi06524323074712609517 #ce Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 10, 2014 Moderators Share Posted November 10, 2014 phatzilla,Can I play too? #include <Array.au3> Local $sString = "abc123d1ef432ghi12345678901234567890" ConsoleWrite($sString & @CRLF) $sNewString = _ReplaceDigits($sString) ConsoleWrite($sNewString & @LF) Func _ReplaceDigits($sString) $aChars = StringSplit($sString, "") For $i = 1 To $aChars[0] $sChar = $aChars[$i] Switch $sChar Case "0" To "9" Do $sNewChar = Chr(Random(48, 57, 1)) Until $sNewChar <> $sChar $aChars[$i] = $sNewChar EndSwitch Next Return _ArrayToString($aChars, "", 1, Default) EndFunc ;==>_ReplaceDigitsAnd like water I would love to know why you want to do this. 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...
Inververs Posted November 10, 2014 Share Posted November 10, 2014 (edited) Cool. But please explain how this works? Local $string = "abc123" $string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _test(\1) & '")) ConsoleWrite($string & @LF) Func _test($in) Return 'b' EndFunc ;==>_Random Return as expected: abcbbb But this: Local $string = "abc123" $string = Execute(StringRegExpReplace("'" & $string & "'", "(\D)", "' & _test(\1) & '")) ConsoleWrite($string & @LF) Func _test($in) Return 'b' EndFunc ;==>_Random return nothing.. D return nothing w return nothing . return nothing only works for d Why? Thank you. Edited November 10, 2014 by Inververs Link to comment Share on other sites More sharing options...
jchd Posted November 10, 2014 Share Posted November 10, 2014 (edited) Another skin for the same cat, no (visible) loop: #include <Array.au3> Local $string = "abc123d1ef432ghi12345678901234567890" ConsoleWrite($string & @LF) $string = Execute(StringRegExpReplace("'" & $string & "'", "(\d)", "' & _NotMe(\1) & '")) ConsoleWrite($string & @LF) Func _NotMe($iVal) Local $a = [0,1,2,3,4,5,6,7,8,9] _ArrayDelete($a, $iVal) Return $a[Random(0, 8, 1)] EndFunc ;==>_NotMe Edited November 10, 2014 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
mikell Posted November 10, 2014 Share Posted November 10, 2014 jc, Inververs' question is very interesting Local $string = "abc123" ;$string = Execute(StringRegExpReplace("'" & $string & "'", "([[:alpha:]])", "' & _test(\1) & '")) $string = Execute(StringRegExpReplace("'" & $string & "'", "([[:alpha:]])", "' & _test('\1') & '")) msgbox(0,"", $string) Func _test($in) Return StringUpper($in) EndFunc This works with w but doesn't work with . or a negated class Link to comment Share on other sites More sharing options...
Malkey Posted November 10, 2014 Share Posted November 10, 2014 Local $string = "abc123" $string = Execute(StringRegExpReplace("'" & $string & "'", "(\D)", "' & _test(\1) & '")) ConsoleWrite($string & @LF) Func _test($in) Return 'b' EndFunc ;==>_Random return nothing.. D return nothing w return nothing . return nothing only works for d Why? Thank you. This works. Local $string = "abc123" $string = Execute("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'") ConsoleWrite("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'" & @LF) ConsoleWrite($string & @LF) Func _test($in) Return 'b' EndFunc ;==>_test #cs Returns:- '' & _test("b") & '' & _test("b") & '' & _test("b") & '123' bbb123 #ce Link to comment Share on other sites More sharing options...
jguinch Posted November 10, 2014 Share Posted November 10, 2014 (edited) Sorry, I read too fast the first topic. Something similar to others : $string = "abc123def432ghi678" $string = Execute ( StringRegExpReplace("'" & $string & "'" , "(\d)", "' & StringMid( StringReplace('0123456789', '\1', ''), Random(1, 9, 1), 1) & '") ) ConsoleWrite ($string) Edited November 10, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Inververs Posted November 10, 2014 Share Posted November 10, 2014 This works. Local $string = "abc123" $string = Execute("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'") ConsoleWrite("'" & StringRegExpReplace($string, "(\D)", "' & _test(""\1"") & '") & "'" & @LF) ConsoleWrite($string & @LF) Func _test($in) Return 'b' EndFunc ;==>_test #cs Returns:- '' & _test("b") & '' & _test("b") & '' & _test("b") & '123' bbb123 #ce now it is clear. Thanks! 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