4

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.