GoJagsGo Posted February 14, 2011 Posted February 14, 2011 Hello folks, I am new to this so please be patient. I have a file, that I need to replace a simple hex string with another hex string. The first part of the string is constant, and the last character is also constant. Everything inbetween is wild. In text it would be "EmailAdress=**********,". I have tried the Funtion "_ReplaceStringInFile.". Works well if you know exactly what to replace, but it does not handle wild cards, from what I can tell. I have also researched the StringRegExpReplace function. This neither seems to fit. Any help would be much appreciated.
wolf9228 Posted February 14, 2011 Posted February 14, 2011 (edited) Hello folks, I am new to this so please be patient. I have a file, that I need to replace a simple hex string with another hex string. The first part of the string is constant, and the last character is also constant. Everything inbetween is wild. In text it would be "EmailAdress=**********,". I have tried the Funtion "_ReplaceStringInFile.". Works well if you know exactly what to replace, but it does not handle wild cards, from what I can tell. I have also researched the StringRegExpReplace function. This neither seems to fit. Any help would be much appreciated. expandcollapse popup$String = StringReplaceBetween("abcdabcdabcd","abcd","abcd","New") MsgBox(0,"Between(abcd,abcd)",$String) $String = StringReplaceBetween("abcdabcdabcd","a","c","New") MsgBox(0,"Between(a,c)",$String) $String = StringReplaceBetween("abcdabcdabcd","a","b","New") MsgBox(0,"Between(a,b)",$String) $String = StringReplaceBetween("aabcdabcdabcd","a","a","New") MsgBox(0,"(aabcdabcdabcd) Between(a,a)",$String) $String = StringReplaceBetween("abcdabcdabcd","ab","c","New") MsgBox(0,"Between(ab,c)",$String) $String = StringReplaceBetween("abcdabcdabcd","b","cd","New") MsgBox(0,"Between(b,cd)",$String) Func StringReplaceBetween($String,$beginString,$EndString,$Replacestring) $CharA = StringUpper(StringLeft($beginString,1)) $CharB = StringUpper(StringLeft($EndString,1)) $Lena = StringLen($String) $LenB = StringLen($beginString) $LenC = StringLen($EndString) $ibeginString = StringUpper($beginString) $iEndString = StringUpper($EndString) Local $i = 1 While ($i <= $Lena) $CharC = StringUpper(StringMid($String,$i,1)) if ($CharA == $CharC) Then $TextA = StringUpper(StringMid($String,$i,$LenB)) If ($TextA == $ibeginString) Then Local $j = $i + $LenB - 1 While ($j <= $Lena) $CharD = StringUpper(StringMid($String,$j,1)) if ($CharB == $CharD) Then $TextB = StringUpper(StringMid($String,$j,$LenC)) if ($TextB == $iEndString) Then if ($i = $j And $LenB = 1) Then $iCharA = StringUpper(StringMid($String,$j,1)) $iCharB = StringUpper(StringMid($String,$j + 1,1)) if ($iCharA <> $iCharB) Then ExitLoop $TextC = StringMid($String,1,$i - 1) $TextD = StringMid($String,$j + 1 + $LenC,$Lena) $String = $TextC & $Replacestring & $TextD $Lena = StringLen($String) $i = StringLen($TextC & $Replacestring) ExitLoop EndIf $TextC = StringMid($String,1,$i - 1) $TextD = StringMid($String,$j + $LenC,$Lena) $String = $TextC & $Replacestring & $TextD $Lena = StringLen($String) $i = StringLen($TextC & $Replacestring) EndIf ExitLoop EndIf $j += 1 WEnd Else ExitLoop EndIf EndIf $i += 1 WEnd Return $String EndFunc Edited February 14, 2011 by wolf9228 صرح السماء كان هنا
wolf9228 Posted February 14, 2011 Posted February 14, 2011 (edited) on this logic better than the previous expandcollapse popup$String = StringReplaceBetween("abcdabcdabcd","abcd","abcd","New") MsgBox(0,"Between(abcd,abcd)",$String) $String = StringReplaceBetween("abcdabcdabcd","a","c","New") MsgBox(0,"Between(a,c)",$String) $String = StringReplaceBetween("abcdabcdabcd","a","b","New") MsgBox(0,"Between(a,b)",$String) $String = StringReplaceBetween("aabcdabcdabcd","a","a","New") MsgBox(0,"(aabcdabcdabcd) Between(a,a)",$String) $String = StringReplaceBetween("abcdabcdabcd","ab","c","New") MsgBox(0,"Between(ab,c)",$String) $String = StringReplaceBetween("abcdabcdabcd","b","cd","New") MsgBox(0,"Between(b,cd)",$String) Func StringReplaceBetween($String,$beginString,$EndString,$Replacestring) $CharA = StringUpper(StringLeft($beginString,1)) $CharB = StringUpper(StringLeft($EndString,1)) $Lena = StringLen($String) $LenB = StringLen($beginString) $LenC = StringLen($EndString) $ibeginString = StringUpper($beginString) $iEndString = StringUpper($EndString) Local $i = 1 While ($i <= $Lena) $CharC = StringUpper(StringMid($String,$i,1)) if ($CharA == $CharC) Then $TextA = StringUpper(StringMid($String,$i,$LenB)) If ($TextA == $ibeginString) Then Local $j = $i + $LenB - 1 While ($j <= $Lena) $CharD = StringUpper(StringMid($String,$j,1)) if ($CharB == $CharD) Then $TextB = StringUpper(StringMid($String,$j,$LenC)) if ($TextB == $iEndString) Then if ($i = $j And $LenB = 1) Then $iCharA = StringUpper(StringMid($String,$j,1)) $iCharB = StringUpper(StringMid($String,$j + 1,1)) if ($iCharA <> $iCharB) Then ExitLoop $TextC = StringMid($String,1,($i + $LenB) - 1) $TextD = StringMid($String,$j + 1,$Lena) $String = $TextC & $Replacestring & $TextD $Lena = StringLen($String) $i = StringLen($TextC & $Replacestring) ExitLoop EndIf $TextC = StringMid($String,1,($i + $LenB) - 1) $TextD = StringMid($String,$j,$Lena) $String = $TextC & $Replacestring & $TextD $Lena = StringLen($String) $i = StringLen($TextC & $Replacestring) EndIf ExitLoop EndIf $j += 1 WEnd Else ExitLoop EndIf EndIf $i += 1 WEnd Return $String EndFunc Edited February 14, 2011 by wolf9228 صرح السماء كان هنا
GoJagsGo Posted February 15, 2011 Author Posted February 15, 2011 Thanks Wolf9228. I will need to experiment with this some. You have introduced me to some new functions that I was not aware of. Some don't seem to show in my help index. Thanks again.
Moderators SmOke_N Posted February 15, 2011 Moderators Posted February 15, 2011 (edited) Is the request as simple as this? Array: #include <String.au3> Global $gs_data = FileRead('SomeFile') Global $ga_between = _StringBetween($gs_data, "unique_front_str", "unique_end_str") Enum through array something like Global $gs_findstr, $gs_replacestr, $gs_myreplacementstr For $i = 0 To UBound($ga_between) - 1 $gs_findstr = "unique_front_str" & $ga_between[$i] & "unique_end_str" $gs_replacestr = "unique_front_str" & $gs_myreplacementstr & "unique_end_str" $gs_data = StringReplace($gs_data, $gs_findstr, $gs_replacestr) Next ; Now $gs_data should have the corrected values ? Edited February 15, 2011 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
GoJagsGo Posted February 18, 2011 Author Posted February 18, 2011 Guys, Both versions worked but I chose the $ga_between script by Smoke_N. Below is what I have so far (bit messy) but it works for the test I have conducted so far. I used it to modified the session file for a webcam application and it worked without any apparent errors. i.e. I was able to pull up the webcam application with the new session file, and see the changed receipient email in various places, and all other functions appeared to be unaffected. Thanks again to all. ------------------------------------------------------------------------------------------------------------- ;Below finds the HEX string to replace, and defines the replacement string variables. $filename = "TEST.txt" Global $gs_data = FileRead($filename) if @error = 1 then MsgBox(0,"File not opened or other error") Else msgbox(0,"AFTER",$gs_data) EndIf ;Hex for (EmailAddress=) and (,"EmailSubject=) Global $ga_between = _StringBetween($gs_data, "456d61696c416464726573733d", "2c22456d61696c5375626a6563743d") if $ga_between = 0 then MsgBox(0, "_StringBetween", "Failure") EndIf if $ga_between = 1 then MsgBox(0,"_StringBetween", "No between found") else MsgBox(0, "StringBetween Is", $ga_between) EndIf ;Hex for the replacement string (dog@catz.com) -Example email address for demo Global $gs_findstr, $gs_replacestr, $gs_myreplacementstr $gs_myreplacementstr = "646f6773406361747a2e636f6d" MsgBox(0,"Replacement String", $gs_myreplacementstr) For $i = 0 To UBound($ga_between) - 1 MsgBox(0,"$ga_between size",UBound($ga_between)) $gs_findstr = "456d61696c416464726573733d" & $ga_between[$i] & "2c22456d61696c5375626a6563743d" MsgBox(0,"gs_findstr", $gs_findstr) $gs_replacestr = "456d61696c416464726573733d" & $gs_myreplacementstr & "2c22456d61696c5375626a6563743d" MsgBox(0,"$gs_replacestr",$gs_replacestr) $find = $gs_findstr $replace = $gs_replacestr $NewFile = _ReplaceStringInFile($filename,$find,$replace, 0, 0) if $NewFile = -1 then msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error) exit else msgbox(0, "INFO", "Found " & $NewFile & " occurances of the pattern: " & $find & " in the file: " & $filename) endif Next FileClose($filename)
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