Regex and a Social Security #

Trancedified

Freshman
Joined
Feb 10, 2004
Messages
27
Location
Highland, CA
I read up on Regular Expressions, can it handle reading through an open application and find a Social Security Number

For example, an SSN would look like:

SSN: 5 5 5-5 5-5 5 5 5

I would like to grab all the underlined numbers and display them like this after I see SSN:

555-55-5555

Any ideas?

Here is the code to "Detect" and look through an open application:

Code:
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic


Friend Class frmSource
	Inherits System.Windows.Forms.Form	
	   
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
        ByVal lpsz2 As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
        ByVal lParam As Integer) As Integer

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByRef lpClassName As String, ByVal lpWindowName As String) As Integer


	Private Const WM_SETTEXT As Short = &HCs
	Private Const WM_GETTEXT As Short = &HDs
	
	Dim hwndTarget As Integer 'target handle
	
    Private Sub btnSend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnSend.Click
        Dim s As String
        s = CStr(txtMsg.Text)
        SendMessage(hwndTarget, WM_SETTEXT, Len(s), s)
    End Sub

    Private Sub frmSource_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim hw As Integer
        Dim cw As Integer
        Dim s As String
        Dim i As Integer

        'first, we need to find the hwnd of the target text box
        btnSend.Enabled = False
        hw = FindWindow(0, "Target Form") 'find the hwnd of the target form
        If hw Then
            cw = FindWindowEx(hw, 0, 0, 0) 'for each child window...
            Do While cw
                s = Space(20)
                i = SendMessage(cw, WM_GETTEXT, 15, s) 'see if the child window is the right one
                If VB.Left(s, i) = "TargetBox" Then
                    hwndTarget = cw
                    btnSend.Enabled = True
                    Exit Do
                Else
                    cw = FindWindowEx(hw, cw, 0, 0) 'look for the next child
                End If
            Loop
        End If
    End Sub
End Class

Any ideas?

Chris
 
I am slightly disturbed that you are grabbing a Social Security Number from another application, especially with the identity theft crisis about.

What are you trying to do? Look at all of the controls that are in the window and check if they have a social security number in it?
Does the number come with spaces between each digit?
 
Last edited:
Yeah a few from other forums said the same, but this is actually a project that's requested from one of our clients. Basically it's a VERY old legacy app that they are using on a Windows 98 box that doesn't have copy and paste. So they would like us to parse through the screen and "get" all the numbers after the heading SSN:

There won't be any spaces, it looks exactly how I posted it previously.
Thanks for your concern about the security issue, this is NOT a hacking program, it's just a tool for our client, the window is already opened by them.


Chris
 
So, you want to validate a number with Regular Expressions? That means this should be in the Regular Expressions forum.
But anyway, I think you can use [0-9] for each digit.

Use [0-9] for each group of digits follow it by {#} the number of digits to match and then put a - hyphen. To make the string at beginning and end, start the string off with ^ and end it with $ (sorry, as with regular expressions, these symbols aren't descriptive in the least), so now you have ^[0-9]{3}-[0-9]{2}-[0-9]{4}$
 
Back
Top