Search code examples
stringgroovysplitregistry

Groovy: Parsing registry value


I have just started writing my first Groovy script, in which I want to read the value of a string from the Windows registry and print the last part of what is returned, to the screen.

This is what I have:

group = Runtime.getRuntime().exec('reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1 -v PatchRebootGroup');
System.out.println "patchRebootGroup=" + group;

The output of the reg command looks like this:

HKEY_LOCAL_MACHINE\SOFTWARE\App\UDF1

PatchRebootGroup REG_SZ Group1

The question is, "How do I print out just 'patchRebootGroup=Group1'?

Thanks.


Solution

  • Here is what I ended up using:

    String output = 'reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\App\\UDF1 -v PatchRebootGroup'.execute().text
    String group = output.tokenize(' ')[-1]
    println 'DesiredPatchRebootGroup=' + group
    return 0