Announcement

Collapse
No announcement yet.

vbs regex ssn search

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

  • vbs regex ssn search

    I am trying to write a vb script that will search through a text file and find an ssn, even if it is enclosed in another string... examples

    blahblah123114633aasdjlasdlasd

    <ssn>123-42-1244</ssn>

    basically ANYWHERE it exists, with or without hyphens.

    I tried this from MS, but it does not work

    Code:
    Const ForReading = 1
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True   
    'objRegEx.Pattern = "\d{9}"
    objRegEx.Pattern = "/^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("test.txt", ForReading)
    strSearchString = objFile.ReadAll
    objFile.Close
    
    Set colMatches = objRegEx.Execute(strSearchString)  
    
    If colMatches.Count > 0 Then
       strMessage = "The following user accounts were found:" & vbCrlf
       For Each strMatch in colMatches   
           strMessage = strMessage &  strMatch.Value & " (character position " &  _
               strMatch.FirstIndex & ")" & vbCrLf
       Next
    End If
    
    Wscript.Echo strMessage
    http://regexlib.com/REDetails.aspx?regexp_id=535

    please help!

  • #2
    Re: vbs regex ssn search

    Code:
    ' http://regexlib.com/REDetails.aspx?regexp_id=535
    '  This regex validates U.S. social security numbers, within
    '  the range of numbers that have been currently allocated. 
    
    ' http://www.regular-expressions.info/reference.html
    ' http://technet.microsoft.com/en-us/magazine/cc137729(TechNet.10).aspx
    
    Const ForReading = 1
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True   
    objRegEx.Pattern = "(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("c:\test.txt", ForReading)
    strSearchString = objFile.ReadAll
    objFile.Close
    
    Set colMatches = objRegEx.Execute(strSearchString)  
    
    Dim iCnt : iCnt = 0
    
    If colMatches.Count > 0 Then
       For Each strMatch in colMatches
            iCnt = iCnt + 1
            strMessage = strMessage & " " & strMatch.Value & vbTab & "(character position" _
               & strMatch.FirstIndex & ")" & vbCrLf 
       Next
       Wscript.Echo "The following", iCnt, "Social Security Numbers were found:" & vbCrlf & strMessage
       
    Else
       wsh.echo "no ssn found"
    End If
    \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

    Working...
    X