Search code examples
regexreplacenotepad++bulk

Notepad++ bulk search and replace string with another string within the same line


I've got a file filled with a bunch of variable declarations looking like this:

//comment line
STR_first_string = "This is the first message to be shown.";
STR_another_message = "Second message here";
STR_string_message_three = "And the third one.";
STR_notification_number_four = "Bla bla yadder yadder";

//another comment line
...

Don't mind the inconsequent naming. It's just to show that the variable names are all different, except for the beginning STR_.

Now, I want to add something to every single line of the file, so that it looks like this:

//comment line
STR_first_string = "This is the first message to be shown."; publicVariable "STR_first_string";
STR_another_message = "Second message here"; publicVariable "STR_another_message";
STR_string_message_three = "And the third one."; publicVariable "STR_string_message_three";
STR_notification_number_four = "Bla bla yadder yadder"; publicVariable "STR_notification_number_four";

//another comment line
...

I informed myself about how to use RegEx in Notepad++. Basically, I intended to replace the terminating ; with publicVariable " and then some fancy regex expression. But I haven't found something matching this particular problem. I'd be really happy if someone could help!

Regards, Stacky


Solution

  • You can try replacing

    ((STR_\S+)\s*=\s*"([^"]|\\")*";)
    

    with

    \1 publicVariable "\2";
    

    \1 will represent part matched by entire regex and \2 represents part matched by (STR_\S+).