Regex for case insensitive registry search

donjkurian

Member
Joined
Oct 6, 2009
Messages
9
Programming Experience
1-3
Hi,
I've recently learnt that Regex class maybe used to develop case insensitive searches. I have a registry search program that will return only the exact matches from the registry. I need to make it case insensitive. Can you guide me how to do that using regular expressions in vb.net 2005. Im posting my code below:

VB.NET:
Sub SearchSubKeys(ByVal root As RegistryKey, ByVal searchKey As String)
        Dim matchtype As String = Nothing
        Dim str(5) As String
        Dim itm As ListViewItem

        totalcount = totalcount + 1
        Label1.Refresh()
        Label2.Refresh()
        TextBox2.Refresh()
        Label1.Text = "No. of keys searched : " + totalcount.ToString
        Label2.Text = "Matching items : " + count.ToString
        TextBox2.Text = "Last key scanned : " + root.ToString

        

        'search key name
        If root.Name.Contains(searchKey) Then

            matchtype = "Key"
            str(0) = matchtype
            str(1) = root.Name
            str(2) = "N/A"
            str(3) = "N/A"
            itm = New ListViewItem(str)

            ListView1.BeginUpdate()
            ListView1.Items.Add(itm)
            ListView1.EndUpdate()
            count = count + 1

        End If

        

        'search value names
        For Each valueName As String In root.GetValueNames

            If valueName.Contains(searchKey) Then
                matchtype = "Value Name"
                str(0) = matchtype
                str(1) = root.Name
                str(2) = valueName
                str(3) = root.GetValue(valueName)
                itm = New ListViewItem(str)
                ListView1.BeginUpdate()
                ListView1.Items.Add(itm)
                ListView1.EndUpdate()
                count = count + 1
            End If
            

            'search values
            Select Case root.GetValueKind(valueName)
                Case RegistryValueKind.String, RegistryValueKind.ExpandString
                    Dim value As String = CStr(root.GetValue(valueName))
                    If value.Contains(searchKey) Then
                        matchtype = "Value"
                        str(0) = matchtype
                        str(1) = root.Name
                        str(2) = valueName
                        str(3) = value
                        itm = New ListViewItem(str)

                        ListView1.BeginUpdate()
                        ListView1.Items.Add(itm)
                        ListView1.EndUpdate()
                        count = count + 1
                    End If
                Case RegistryValueKind.MultiString
                    Dim value As String = String.Join(vbNewLine, CType(root.GetValue(valueName), String()))
                    If value.Contains(searchKey) Then
                        matchtype = "Value"
                        str(0) = matchtype
                        str(1) = root.Name
                        str(2) = valueName
                        str(3) = value
                        itm = New ListViewItem(str)

                        ListView1.BeginUpdate()
                        ListView1.Items.Add(itm)
                        ListView1.EndUpdate()
                        count = count + 1
                    End If
            End Select

        Next

        

        For Each subkeyName As String In root.GetSubKeyNames
            Try
                Dim subkey As RegistryKey = root.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadSubTree, Security.AccessControl.RegistryRights.ReadKey)
                SearchSubKeys(subkey, searchKey)
                subkey.Close()
            Catch ex As Exception
                
            End Try
        Next
    End Sub
 

donjkurian

Member
Joined
Oct 6, 2009
Messages
9
Programming Experience
1-3
HI,
Thank You. Is there anything else i need to add along with 'Option Compare Text' because that alone does not work for me. For ex: If i search for the keyword 'WORD', the search returns only instances of 'WORD', it doesn't return 'word' or 'Word' etc....Please suggest..

Thanks,
donjkurian
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
{Be hAppy}

If you are adding:

Option Compare text

then

msgbox instr("AbcDef","cde") will return TRUE

otherwise it will return FALSE.

Have you tried after adding this ....
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
{Be hAppy}

Just now I discovered that Contains function does not work under option compare text.

So change your this line:

If root.Name.Contains(searchKey) Then

as

If instr(root.Name,searchKey)<>0 Then


But don't forget to add option compare text at the top of the module.
 

send2te

Well-known member
Joined
Dec 13, 2009
Messages
46
Programming Experience
10+
{Be hAppy}

Alternate way (if you don't want to use Option Compare Text) is:

If root.Name.ToLower.Contains(searchKey.ToLower) Then
 
Top Bottom