tags:

views:

45

answers:

1

I am writing my first python program and I am running into a problem with regex. I am using regular expression to search for a specific value in a registry key.

import _winreg
import re

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{26A24AE4-039D-4CA4-87B4-2F83216020FF}")

results=[]
v = re.compile(r"(?i)Java")

try:
    i = 0
    while 1:
        name, value, type = _winreg.EnumValue(key, i)
        if v.search(value):
         results.append((name,value,type))
        i += 1
except WindowsError:
    print

for x in results:
 print "%-50s%-80s%-20s" % x

I am getting the following error:

exceptions.TypeError: expected string or buffer

I can use the "name" variable and my regex works fine. For example if I make the following changes regex doesn't complain:

v = re.compile(r"(?i)DisplayName")

if v.search(name):

Thanks for any help.

+2  A: 

The documentation for EnumValue explains that the 3-tuple returned is a string, an object that can be any of the Value Types, then an integer. As the error explained, you must pass in a string or a buffer, so that's why v.search(value) fails.

You should be able to get away with v.search(str(value)) to convert value to a string.

Mark Rushakoff
That worked thanks!
spaghettiwestern
Actually I had some problems using str with non ascii characters in the registry. After messing around with encode and getting mixed results I found that simply using repr in place of your str suggestion fixed my problem. Not sure if this is proper or not but it works.
spaghettiwestern