两个文件之间的字符串匹配

来源:互联网 发布:百度 mysql 编辑:程序博客网 时间:2024/05/01 18:56

用第一个文件中的每行字符串为pattern,在第二个文件中寻找是否有匹配的行

 Public Function matchInTwoFiles(file1,file2)
     matchInTwoFiles = True
     Dim fso, fileobj1, fileobj2, stream1, stream2, regEx,Match, Matches
     Set fso = CreateObject("Scripting.FileSystemObject")
     Set fileobj1 = fso.GetFile(file1)
     Set fileobj2 = fso.GetFile(file2)
     Set stream1 = fileobj1.OpenAsTextStream(1)   
     Set stream2 = fileobj2.OpenAsTextStream(1)
     Set regEx = New RegExp
     Do   While Not stream1.AtEndOfStream
         str1 = stream1.ReadLine
         match = False
         stream2.Close
         Set stream2 = fileobj2.OpenAsTextStream(1)
         Do While Not stream2.AtEndOfStream and match=False
           str2 = stream2.ReadLine
           match = RegExpTest(str1,str2)
         Loop
         match1 = match
         matchInTwoFiles = matchInTwoFiles and match1        
     Loop
     stream1.Close
     stream2.Close
     Set stream1 = Nothing
     Set stream2 = Nothing
     Set fileobj1 = Nothing
     Set fileobj2 = Nothing
     Set fso = Nothing
     Set regEx = Nothing
End Function

 

Function RegExpTest(patm,strng)
  Dim regEx,retVal
  Set regEX = New RegExp
  regEx.Pattern=patm
  regEx.IgnoreCase=False
  regEx.Global = True
  retVal = regEx.Test(strng)
  If retVal Then
     RegExpTest = True
     Else
     RegExpTest = False
  End If
End Function