Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: brew on March 20, 2007, 06:57 PM

Title: Search text files
Post by: brew on March 20, 2007, 06:57 PM
Is there any efficient and easy way to find a certain string in a file besides....

Dim asdf$
Open file For Input As #1
Do Until EOF(1)
Line Input #1, asdf
If asdf = "string" Then Goto Lol
Loop
Lol:
Close #1

which is what i have been using for all the while?
Title: Re: Search text files
Post by: MyndFyre on March 20, 2007, 07:07 PM
Yes, you can use Regular Expressions.  Take a look at http://visualbasic.about.com/od/usingvbnet/l/blregexa.htm (http://visualbasic.about.com/od/usingvbnet/l/blregexa.htm) for a brief stint in their use in VB6 as well as http://www.regular-expressions.info/vb.html (http://www.regular-expressions.info/vb.html) which as a site is a great reference for regex.

Regular expressions are also supported by the Microsoft .NET Platform and are consequently available in Visual Basic .NET.
Title: Re: Search text files
Post by: Barabajagal on March 20, 2007, 07:25 PM
Or load the entire file using binary access read, and then use instr.
Title: Re: Search text files
Post by: brew on March 20, 2007, 08:08 PM
Myndfyre, thanks for helping. RegExs look very interesting and i might use that... but it would still require me to open the file as binary in order to use it. and that's basically what reality said......
Quote from: [RealityRipple] on March 20, 2007, 07:25 PM
Or load the entire file using binary access read, and then use instr.
because somewhere in that article it goes on to state how InStr() and Like() (like is good, but it's only for ranges of ascii characters) are the only two native VB6 regex functions. SO my best bet is to do what reality said, open it in binary and use InStr() until i find more documentation about and gain deeper knowledge of regexes.
Title: Re: Search text files
Post by: Barabajagal on March 20, 2007, 08:41 PM
Well, there is one ity bity problem with that method. Depending on the file you load, it will take more time, ram, and process usage.
Title: Re: Search text files
Post by: UserLoser on March 20, 2007, 10:25 PM
Quote from: brew on March 20, 2007, 08:08 PM
Myndfyre, thanks for helping. RegExs look very interesting and i might use that... but it would still require me to open the file as binary in order to use it. and that's basically what reality said......
Quote from: [RealityRipple] on March 20, 2007, 07:25 PM
Or load the entire file using binary access read, and then use instr.
because somewhere in that article it goes on to state how InStr() and Like() (like is good, but it's only for ranges of ascii characters) are the only two native VB6 regex functions. SO my best bet is to do what reality said, open it in binary and use InStr() until i find more documentation about and gain deeper knowledge of regexes.

Note that Like isn't a function, it's an operator
Title: Re: Search text files
Post by: brew on March 21, 2007, 09:23 AM
Its a crazy one! Infact, too crazy to be considered an operator! heh... it should be a function though.