Discussion Locked

Pattern Matching (Text search)

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I am looking for information (books, articles, discussion forums, etc.) on pattern matching recognition (i.e.: how to search a bunch of files and find a text in them). My programming language is Visual Basic, but the ode might be written in any other language provided it can be called as a subroutine by Visual Basic. Thanks for your help

3 replies

by andrew

You may want to check out Regular Expressions. Here's a link to an MSDN article about using it with VB:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbdev99/html/VB99I1.asp

Here's some sample code that recursively searches a folder, looking for any file that contains a social security number (a pattern like '111-22-3333')

Private Sub RecurseFolders(sStartPath As String)
Dim oFSO As Scripting.FileSystemObject
Dim oFolders As Scripting.Folders
Dim oFolder As Scripting.Folder
Dim oSubFolder As Scripting.Folder
Dim oFiles As Scripting.Files
Dim oFile As Scripting.File
Dim oTs As Scripting.TextStream
Dim oRegExp As VBScript_RegExp_55.RegExp

Set oFSO = New Scripting.FileSystemObject
Set oFolder = oFSO.GetFolder(sStartPath)
Set oFiles = oFolder.Files
Set oRegExp = New VBScript_RegExp_55.RegExp
oRegExp.Pattern = "[0-9]{3}\-[0-9]{2}\-[0-9]{4}"
For Each oFile In oFiles
Set oTs = oFile.OpenAsTextStream(ForReading) Do
If oRegExp.Test(oTs.ReadLine()) Then
List1.AddItem oFile.Path & "\" & oFile.Name
Exit Do
End If
Loop Until oTs.AtEndOfStream
oTs.Close
Next

Set oFolders = oFolder.SubFolders
For Each oSubFolder In oFolders
RecurseFolders oSubFolder.Path
Next
End Sub

Perl is a great language to regular expressions. O'Reilly's book is excellent:

http://www.oreilly.com/catalog/regex/

Hope this is what you're looking for.

(make sure there are no spaces in the URLs)

This discussion is closed to new replies.