-1

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

Stacky
  • 875
  • 9
  • 24

2 Answers2

1

It sounds like you just want to replace:

(STR_\S+)\s*=\s*".*";

...with:

\0 publicVariable "\1";
Russell Zahniser
  • 16,188
  • 39
  • 30
  • +1, but while general idea is correct not all Notepad++ version support `\0` group reference so consider surrounding regex with parenthesis to place it in group 1 and as replacement use `\1 publicVariable "\2";`. – Pshemo Apr 23 '14 at 13:58
  • Unfortunately, the first regex considers the whole file as one single match, since it doesn't know what `*` is and thereby doesn't know that this regex should be used line by line. So this regex marks a text block, starting at the very first `STR_` and ends at the very last `;`. – Stacky Apr 23 '14 at 14:05
  • @Stacky Strange, I don't have this problem. Well in that case don't use `".*"` but `"[^"]*"` (I assume that your strings don't have some inner escaped `"`). – Pshemo Apr 23 '14 at 14:10
  • Alright, the regex now recognizes every single line. But I believe, I use the replace term incorrectly. To clearify, the search statement marks the whole line. But I don't want to replace the whole line, I only want to add the variable name (beginning with `STR_`) to be in the quotes of the `publicVariable` declaration. And that still doesn't work yet. – Stacky Apr 23 '14 at 14:19
  • @Stacky `the search statement marks the whole line. But I don't want to replace the whole line` That is OK. This regex should replace whole line with exactly same line plus additional values including variable name after `STR_`. – Pshemo Apr 23 '14 at 14:24
  • Using `\0 publicVariable "\1";` simply deletes the whole line. Using `\1 publicVariable "\2";` gives me the following result: `STR_first_string publicVariable "";` I don't know what I'm doing wrong... – Stacky Apr 23 '14 at 16:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51294/discussion-between-stacky-and-pshemo) – Stacky Apr 23 '14 at 16:45
1

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+).

Pshemo
  • 122,468
  • 25
  • 185
  • 269