Search for a String within a text file?

Hello Everyone,

In need of a little help and hope someone will be able to shed some light on this. I want to basically read a text file and search each line of the file to check if it contains a certain string. If the line contains the string, write the entire line that contains the string to a TextArea and ignore the line if it does not contain the string.

I have read the docs on TextInputStream and can open the file and read the entire text file to a TextArea but I am at a loss on how to parse (filter) the file to see only the lines that contain the specified string.

Thanks

Use TextInputStream.ReadLine in a loop, then search each line using InStr. e.g.:

While Not MyTextInputStream.EOF Dim line As String = MyTextInputStream.ReadLine If InStr(line, "Search String") > 0 Then ' the line contains the search string End If Wend

Andrew’s reply is correct. Here are a couple of alternatives:

dim text as string = MyTextInputStream.ReadAll
dim lineArr() as string = text.Split( EndOfLine )
dim matchLines() as string
for lineIndex as integer = 0 to lineArr.Ubound
dim thisLine as string = lineArr( lineIndex )
if thisLine.InStr( searchString ) <> 0 then
matchLines.Append thisLine
end if
next lineIndex

TextArea1.Text = join( matchLines, EndOfLine )

With a regular expression:

dim text as string = MyTextInputStream.ReadAll
dim matchLines() as string

dim rx as new RegEx
rx.SearchPattern = "^.*\\Q" + searchString + "\\E.*$"

dim match as RegExMatch = rx.Search( text )
while match <> nil
matchLines.Append match.SubExpressionString( 0 )
match = rx.Search
wend

TextArea1.Text = join( matchLines, EndOfLine )

Thank you both for the help, it’s much appreciated!!

I would suggest using the RegEx option. I am parsing OFX and QIF (Bank Statements) at the moment and started using InStr and Mid functions. I then converted to using RegEx and the performance increase was phenomenal. Depends on the size of the file but always bear in mind your test case should be worse case, your customer/end user may need to parse vast quantities of data.

I talked about this during my Xojo sessions. Yes, there is a definite performance benefit in using a RegEx the longer the target text is. For shorter targets, that benefit is washed out by the time it takes to set up the RegEx.

Yes I agree Kem. RegEx can cause a serious migraine to get right but its worth it once you get it right. I was testing my app on my downloaded OFX bank statements with 10,20,30 transactions and then I thought I better test for a few thousand transactions. This is where I went from 10’s of seconds to a few seconds to parse using RegEx.

Actually, looking again, I have to modify that search pattern for the unlikely event that the search string contains “\E”:

rx.SearchPattern = "^.*\\Q" + searchString.ReplaceAllB( "\\E", "\\\\EE\\Q" ) + "\\E.*$"

Hallo everyone , nice post , I have one question too , i think it will be a simple code but i just can’t figure it out in visual basic it was a little different , but any way ,
so on mac os x with Xojo i want to read a text file , so just i have my windows i have a text field there and when i click on the button i want to load a tex file or file with no extention to load on that tex area on my program .
is there a siple line code for that ?

for exaple in Visual basic I was using something like this :

Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText("C:\\test.txt") textbox1.text = filereader

Look in the Language Reference for TextInputStream. The code would look something like this:

dim f as new FolderItem( "path:to:file" )
dim tis as TextInputStream = TextInputStream.Open( f )
TextField1.Text = tis.ReadAll

In the future, please start a new conversation instead of posting in an unrelated one. You will get better responses.

How about the footstep RAM addition to your application when you use RegEx ?