Search for section of text and delete.

  1. last week

    Niles M

    Sep 10 Pre-Release Testers, Xojo Pro

    Apologies if this information is elsewhere in the forum, I couldn't find it.

    I'd like to know how to search for a section of text in a file that starts with a certain phrase and ends with another and delete them and everything in between.

    Example.

    #beginning phrase
    Blah, blah, blah, blah, blah
    #ending phrase

    That code never assigns the text within the file to the variable s.

    t = TextInputStream.Open(f)
    dim s as string = t.ReadAll
    t.Close
    
    dim rx ...
    ...
  2. Dave S

    Sep 10 San Diego, California USA

    in a text area?
    here is a brute force method

    dim s as string="BEGIN YOUR TEXT"
    dim t as string="END OF TEXT"
    
    x=instr(ta.text,s)
    y=instr(x,ta.text,t)
    if x>0 and y>x then 
    ta.selstart=x
    ta.sellenght(=y-x)+len(t)
    ta.seltext=""
  3. Emile S

    Sep 10 Europe (France, Strasbourg)

    Load the text file in the TtextArea using the LR Example,
    follow Dave advice code,
    Save the text from the TextArea.

    Go to TextInputStream and TextOutputStream for details.

  4. Kem T

    Sep 10 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut
    dim rx as new RegEx
    rx.SearchPattern = "#beginning phrase\b[\s\S]*#ending phrase\b"
    rx.ReplacementPattern = ""
    rx.Options.ReplaceAllMatches = true
    rx.Options.Greedy = false
    
    s = rx.Replace( s )
  5. Niles M

    Sep 10 Pre-Release Testers, Xojo Pro

    Dave, you didn't define all your variables, so I wasn't sure how to proceed with a test.

    Kem, I tried your method but couldn't get it to work. Here's my code.

      Dim dlg As New OpenDialog
      dlg.ActionButtonCaption = "Select"
      dim f as FolderItem
      Dim t as TextInputStream
      
      
      dlg.Title = "Choose File"
      
      Dim docType as New FileType
      docType.Name = "Files"
      dlg.Filter=docType
      dlg.MultiSelect=True
      dlg.InitialDirectory=SpecialFolder.Desktop
      f = dlg.ShowModal
      
      If f = Nil Then
        return
      End
      
      t = TextInputStream.Open(f)
      dim s as string
      
      dim rx as new RegEx
      rx.SearchPattern = "#Start\b[\s\S]*#Finish\b"
      rx.ReplacementPattern = ""
      rx.Options.ReplaceAllMatches = true
      rx.Options.Greedy = false
      
      s = rx.Replace( s )
      
      
      t.Close
  6. Kem T

    Sep 10 Pre-Release Testers, Xojo Pro, XDC Speakers Answer Connecticut

    That code never assigns the text within the file to the variable s.

    t = TextInputStream.Open(f)
    dim s as string = t.ReadAll
    t.Close
    
    dim rx ...
    ...
  7. Dave S

    Sep 10 San Diego, California USA

    @Niles M Dave, you didn't define all your variables, so I wasn't sure how to proceed with a test.

    I assumed you were experienced enough to have figured that bit out.... sorry that I was incorrect

  8. Niles M

    Sep 10 Pre-Release Testers, Xojo Pro

    Thank you Kem! This worked perfectly! You rock!

or Sign Up to reply!