Results 1 to 4 of 4

Thread: Read text and replace words in side

  1. #1

    Read text and replace words in side

    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

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Read text and replace words in side

    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Read text and replace words in side

    Heres a quick case sensitive version

    [code=thinbasic]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
    [/code]

    Attached Files Attached Files
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Read text and replace words in side

    And this is using regular expressions:

    [code=thinbasic]
    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
    [/code]
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. reverse the words
    By zak in forum General
    Replies: 2
    Last Post: 25-01-2011, 22:55
  2. RichEdit, searching & coloring words
    By zak in forum General
    Replies: 4
    Last Post: 31-08-2010, 14:15

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •