Search code examples
regexwindowsregistrynotepad++

regex registry replace


I have bunch of windows registry .reg files and would like to make some changes into them, replacing SID with HKCU.

files look like this:

[-HKEY_USERS\S-1-5-21-**********-********-*********-500\SomeKey\SomeSubKey]
blah blah blah
[-HKEY_USERS\S-1-5-21-**********-********-*********-500_Classes\SomeKey\SomeSubKey]
[HKEY_USERS\S-1-5-21-**********-********-*********-500\SomeKey\SomeSubKey]
blah blah blah
[HKEY_USERS\S-1-5-21-**********-********-*********-500_Classes\SomeKey\SomeSubKey]

and the expected result would be like this:

[-HKEY_CURRENT_USER\SomeKey\SomeSubKey]
blah blah blah
[-HKEY_CURRENT_USER\Software\Classes\SomeKey\SomeSubKey]
[HKEY_CURRENT_USER\SomeKey\SomeSubKey]
blah blah blah
[HKEY_CURRENT_USER\Software\Classes\SomeKey\SomeSubKey]

I basically know what should be done but as regex is not my language maybe someone will help me :)

The idea is to search at the beginning of the line for [HKEY or [-HKEY continue until *-500 replace HKEY* until *-500 with HKEY\. Of course there is 500 vs 500_Classes and Software thing, but if given right direction I could figure out that myself or do a double run. I would use Notepad++ for this.


Solution

  • Find

    \[(-)?HKEY_USERS\\S-1-5-21-.{10}-.{8}-.{9}-500(_Classes)?(.*)
    

    and replace with

    [$1HKEY_CURRENT_USER(?{2}\\Software\\Classes)$3