Hex String - Wildcard replace?
#1
Posted 14 February 2011 - 12:40 AM
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.
#2
Posted 14 February 2011 - 05:43 AM
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.
$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 by wolf9228, 14 February 2011 - 11:04 AM.
صرح السماء كان هنا
#3
Posted 14 February 2011 - 05:53 AM
$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 by wolf9228, 14 February 2011 - 11:00 AM.
صرح السماء كان هنا
#4
Posted 15 February 2011 - 03:04 AM
#5
Posted 15 February 2011 - 08:30 AM
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 by SmOke_N, 15 February 2011 - 08:31 AM.
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.
#6
Posted 18 February 2011 - 05:19 AM
-------------------------------------------------------------------------------------------------------------
;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)
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users





