Search for section of text and delete.

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

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=""

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.

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 )

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.

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

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 ...
...

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

Thank you Kem! This worked perfectly! You rock!