Jump to content
Sign in to follow this  
bhodi78

How can I use "_ReplaceStringInFile()" for matching whole word?

Recommended Posts

bhodi78

I whont replace all whole word of the "a.txt" file

example of a.txt file:

My mommy always said|there were no monsters.
She's operating at|a completely adult capacity.
-What about her memories? |There are gaps. Synaptic dissonance.
She's freaked!

#include <Array.au3>
#include <File.au3>

$nameS="C:\a.txt"
$nameD="C:\a(TT).txt"
FileCopy($nameS,$nameD,1) ;overwrite existing files

Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"]
Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"]

For $i=0 To UBound($Wordslist)-1
    $str=" "&$Wordslist[$i]&"("&$translated[$i]&") "
    $w  =" " & $Wordslist[$i] & " "
    $retval = _ReplaceStringInFile($nameD,$w,$str)
    If $retval = -1 then
        msgbox(0, "ERROR", "Not be replaced in file")
        Exit
    Else
        ;MsgBox(0,$w,$str)
    Endif
Next

MsgBox(0, "Finish!","Finish!")

the result is:

My mommy(mamma) always said|there were no monsters.
She's operating at|a completely adult capacity.
-What about her memories? |There are gaps. Synaptic dissonance.
She's freaked!

The result is not what I wanted, in fact the words ("monsters","what","memories","There","freaked","my") have not been replaced. I solved the problem in the following code, but is very slow for a large list of words. Have a better solution?

#include <Array.au3>
#include <File.au3>

Local $myChar[11]=["'","-","!","?",",",";",":"," ","}",".","|"]
$nameS="C:\a.txt"
$nameD="C:\a(TT).txt"
FileCopy($nameS,$nameD,1) ;overwrite existing files

Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"]
Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"]

For $i=0 To UBound($Wordslist)-1
    For $cs In $myChar
        For $ce In $myChar
            $w  =$cs & $Wordslist[$i] & $ce
            $str=$cs & $Wordslist[$i]&"("&$translated[$i]&")" & $ce
            $retval = _ReplaceStringInFile($nameD,$w,$str)
            If $retval = -1 then
                msgbox(0, "ERROR", "Not be replaced in file")
                Exit
            Else
                ;MsgBox(0,$w,$str)
            Endif
        Next
    Next
Next

MsgBox(0, "Finish!","Finish!")

the result is:

My mommy(mamma) always said|There(là) were no monsters(mostro).
She's operating at|a completely adult capacity.
-what(che cosa) about her memories(ricordi)? |There(là) are gaps. Synaptic dissonance.
She's freaked(anormale)!

Note that the first word ("My") has not yet replaced. I hope you can help me thanks.

Bye

Share this post


Link to post
Share on other sites
dantay9

Here is a way that will get you by. It isn't perfect at all though.

#include <Array.au3>
#include <File.au3>

Global $Wordslist[7] = ["monsters", "what", "memories", "There", "freaked", "my", "mommy"]
Global $translated[7] = ["mostro", "che cosa", "ricordi", "là", "anormale", "mio", "mamma"]
Global $nameS = "a.txt"
Global $nameD = "a(TT).txt"

$Output = FileRead($nameS)

For $i = 0 To UBound($Wordslist) - 1
    $Output = StringReplace($Output, $Wordslist[$i], $translated[$i])
Next

If FileExists($nameD) Then FileDelete($nameD)
FileWrite($nameD, $Output)

If you want something that is perfect, you should probably use StringRegExp. It is a bit difficult in the beginning, but once you understand it, it can prove very useful.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
99ojo

Here is a way that will get you by. It isn't perfect at all though.

#include <Array.au3>
#include <File.au3>

Global $Wordslist[7] = ["monsters", "what", "memories", "There", "freaked", "my", "mommy"]
Global $translated[7] = ["mostro", "che cosa", "ricordi", "là", "anormale", "mio", "mamma"]
Global $nameS = "a.txt"
Global $nameD = "a(TT).txt"

$Output = FileRead($nameS)

For $i = 0 To UBound($Wordslist) - 1
    $Output = StringReplace($Output, $Wordslist[$i], $translated[$i])
Next

If FileExists($nameD) Then FileDelete($nameD)
FileWrite($nameD, $Output)

If you want something that is perfect, you should probably use StringRegExp. It is a bit difficult in the beginning, but once you understand it, it can prove very useful.

Hi,

here we go:

#include <File.au3>

$nameS="C:\a.txt"
$nameD="C:\a(TT).txt"

Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"]
Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"]
Local $arfile
_FileReadToArray ($nameS, $arfile)
For $i = 1 To UBound ($arfile) - 1
    For $j = 0 To UBound ($Wordslist) - 1
        $pattern = "((?i)\b" & $Wordslist [$j] & ")"
        $var = StringRegExpReplace ($arfile [$i], $pattern,  $Wordslist [$j] & "(" & $translated [$j] & ")")
        $arfile [$i] = $var
    Next
Next
_FileWriteFromArray ($nameD, $arfile, 1)
MsgBox(0, "Finish!","Finish!")
ShellExecute ("notepad.exe", $nameD)

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites
bhodi78

Thanks to all,

the solution with StringRegExpReplace is excellent.

:):)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×