PDA

View Full Version : Read text and replace words in side



Henry
10-09-2007, 13:02
Hi, there i would like to know if any could help me

i want to make a script that would replace words in a text document

E.G

()=is the original
""=replacement

(one) replace "three"

i want to do that to a lot of words in one text

thanks kindly regards Henry

ErosOlmi
10-09-2007, 13:24
You need to use regular expressions.
I will make an example when back from work if no-one will have posted something before.

Ciao
Eros

Michael Clease
10-09-2007, 14:43
Heres a quick case sensitive version


uses "FILE"

DIM OldString AS STRING VALUE "old"
dim NewString as STRING VALUE "NEW"
DIM FileBuff as STRING VALUE ""
DIM sFile as STRING VALUE "test.txt"
DIM Result as DWORD VALUE 0

FileBuff = file_LOAD(sFile) ' Load the FIle
if filebuff = "" then msgbox 0, "No File" : STOP

FileBuff = Replace$( FileBuff, OldString, NewString)
Result = FILE_SAVE(App_SourcePath+"Test2.txt", FileBuff)

if Result = 0 then Msgbox 0, "File Saved OK"
else MSGBOX 0, "File Save Error"
ENDIF

STOP

ErosOlmi
10-09-2007, 16:00
And this is using regular expressions:



uses "VBREGEXP"

dim lpRegExp as dword
dim strText as string value repeat$(2, "The quick brown (fox) (jumped) over the lazy (dog) (aaa)." & $crlf)
dim strRetVal as string

'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New

'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
MSGBOX 0, "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
stop
end if

'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1


'---Replace example 1
VBREGEXP_SetPattern lpRegExp, "\((\S+|\s+)\)"
strRetVal = VbRegExp_Replace(lpRegExp, strText, """$1""")

MSGBOX 0, strRetVal