Announcement

Collapse
No announcement yet.

Search more than one RegEx Pattern

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search more than one RegEx Pattern

    Hi Guys,

    I'm not very experienced with using regex, and have not had any luck with research...How would I search for more than 1 regex pattern? All patterns do NOT have to match, but if at least 1 pattern does out of the multiple specified, then it would be a match:

    Code:
    Const ForReading = 1
    
    Dim objFSO, objDictionary
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDictionary = CreateObject("Scripting.Dictionary")
    
    ' set regex patterns (multiple patterns??)
    objRegEx.Global = True   
    objRegEx.Pattern = "Open=system3_.exe"
    
    ' search for autorun.inf w/ pattern matching, and delete
    strAutoinf = "autorun.inf"
    
    FindFile("C:\")
    
    Z = objDictionary.Count
    If Z > 0 Then
       For Each strFile In objDictionary
          Set objFile = objFSO.OpenTextFile(strFile, ForReading)
          strSearchString = objFile.ReadAll
          objFile.Close
          Set colMatches = objRegEx.Execute(strSearchString)
          If colMatches.Count > 0 Then
             For Each strMatch in colMatches
                On Error Resume Next
                objFSO.DeleteFile strFile
                If (Err.Number <> 0) Then
                  msgbox "Unable to delete " & srtFile
                End If
                On Error GoTo 0
             Next
          End If
       Next
    End If
    
    Sub FindFile(ThisFolder)
        Dim File
        Dim Folder
        For Each Folder In objFSO.GetFolder(ThisFolder).SubFolders
           For Each File In Folder.Files
              If LCase(File.Name) = strAutoinf Then
    	     objDictionary.Add File
              End If
           Next
           Call FindFile(Folder)
        Next
    End Sub

  • #2
    Re: Search more than one RegEx Pattern

    I guess if I had to do it another way, I could read each line into an array from the files, and use the "InStr" function to match multiple lines...like:

    Code:
    strSearchString = objFile.ReadAll
    arrLines = Split(strSearchString, vbNewLine)
    
    For Each strLine in arrLines
       If InStr(strLine, "Open=system3_.exe") OR _
          InStr(strLine, "Shellexecute=system3_.exe") Then
             On Error Resume Next
             objFSO.DeleteFile strFile
             If (Err.Number <> 0) Then
                msgbox "Unable to delete " & srtFile
             End If
             On Error GoTo 0
       End If
    Next
    I don't really know which way is better since I don't know the regex method yet
    Last edited by ekrengel; 9th April 2010, 12:22.

    Comment


    • #3
      Re: Search more than one RegEx Pattern

      You almost got the right operator, that is, or.

      In regexp, group statements and or 'em:

      Code:
      (?:(Open=)|(Shellexecute=))(system3_.exe)
      In English, it reads: "match either Open= or Shellexecute= and put that into capture group 1. After that, match system3_.exe and put that into capture group 2.

      The outmost parenthesis (?: ) are non-capturing, so that the Open/Shell part is captured only once.

      Regexes are quite simple, but the weird syntax often confuses people. Here are some good resources: http://www.regular-expressions.info/ and a great book too.

      -vP

      Comment


      • #4
        Re: Search more than one RegEx Pattern

        Ah ok cool thanks. Now I'm getting somewhere

        So both of what I listed before only have to be captured once for the file to be a match, so it would be like this??

        Code:
        objRegEx.Pattern = "(?:(Open=system3_.exe)|(Shellexecute=system3_.exe))"

        Edit - A really good tutorial that just explained a lot for me too: http://www.4guysfromrolla.com/webtech/090199-1.shtml although regular-expressions.info is much better now that I'm going through it

        OMG backreferences are making my head spin
        Last edited by ekrengel; 9th April 2010, 19:13.

        Comment


        • #5
          Re: Search more than one RegEx Pattern

          Originally posted by ekrengel View Post
          OMG backreferences are making my head spin
          Try Regex Coach for easy visualizing. It's an interactive regex tool and free too. You type an expression into a text field and text into another. Then it automagically highlights the text according your expression. It works in real-time, so any changes in text or expression is effective immediately.

          -vP

          Comment


          • #6
            Re: Search more than one RegEx Pattern

            Originally posted by ekrengel View Post
            So both of what I listed before only have to be captured once for the file to be a match, so it would be like this??

            Code:
            objRegEx.Pattern = "(?:(Open=system3_.exe)|(Shellexecute=system3_.exe))"
            Correct (you can test patterns here, http://www.regular-expressions.info/...ptexample.html)

            Additionally, you may also like to
            - make the RegExp Tests case insensitive ( .IgnoreCase = True )
            - insert 'word boundaries' ( \b ) for matching the full word only.
            - allow for spaces typed around the "=" sign ( ([ \t]+)? )
            - skip maching strings when commented-out ( ; ) in the file.

            Here's a sample:
            Code:
            Const ForReading = 1
            
            Dim oRE
            
            strFile = "C:\path\txtfile.inf"
            
            strPattern = "\b(?:Open|Shellexecute)([ \t]+)?=([ \t]+)?system3_.exe\b"
            
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            
            If objFSO.FileExists(strFile) Then
               On Error Resume Next
            
               Set objFile = objFSO.OpenTextFile(strFile, ForReading)
               arrLines = Split(objFile.ReadAll, vbCrLf)
               objFile.Close : Set objFile = Nothing
            
               Set oRE = New RegExp : oRE.Global = True
               oRE.Pattern = strPattern
               oRE.IgnoreCase = True
            
               For Each strLine in arrLines
            
               ' If oRE.Test(strLine) = True Then
                 ' included an additional check if perhaps the string is commented-out...
                     Set matches = oRE.Execute(strLine)
                     pos = matches(0).FirstIndex + 1
                     com = inStr(strLine, ";")
                     If matches.Count > 0 AND NOT _       
                       (com > 0 AND com < pos) Then
            
                   err.Clear
                   objFSO.DeleteFile strFile
                   If (Err.Number <> 0) Then
                     msgbox "Unable to delete " & strFile
            
                   Else
                     wsh.echo "Deleted successfully " & strFile
                   End If
                   iFnd = TRUE
                   Exit For
                 End If
                 iFnd = FALSE
               Next
            
               If iFnd = FALSE Then _
                 wsh.echo "search string not present in " & strFile
            
               On Error Goto 0
            Else
               wsh.echo "File Not found " & strFile
            End If
            
            wsh.quit 0
            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Search more than one RegEx Pattern

              Thnx! This helps a lot!

              Comment

              Working...
              X