Sublime Forum

Replace "string" to (abc\def\"string")

#1

Hi to all,

I’m trying to contain a string into another set of string where the “container” is fixed, but the string inside is variable.

Examples of strings:

amb_cold.ogg
amb_lava.ogg

Example of container (one for all):

FMODSoundAdd("sounds\\chars\\amb\\[string]",0,0);

Desired replace results:

FMODSoundAdd("sounds\\chars\\amb\\amb_cold.ogg",0,0);
FMODSoundAdd("sounds\\chars\\amb\\amb_lava.ogg",0,0);

With the search/replace function, to capture the initial string, I used under Find What: .\w+.*

However, I’m having trouble with executing the replace part. What exactly needs to go there to make it replace in a manner that achieves the above result? The basic solution I tried was using =FMODSoundAdd(“sounds\chars\amb\(.\w+.*)”,0,0); but that doesn’t work.

Thanks for any input.

0 Likes

#2

You need to use capture group in the find (by using parenthesis) and then use \1 (\2, … when there is multiple capture group) to reference what was capture in the find part.
Also I’m not sure about your usage of the dot character: when in a regular expression it means anything, so your find pattern seems a bit strange to me.
If you are looking for what seems to be filename the regular expression would be something like (\w+\.\w+) and then your replace would be FMODSoundAdd(“sounds\\chars\\amb\\\1”,0,0); (you need to escape the backslash character)

1 Like

#3

Thanks Clams.

The adjusted code works exactly as intended.

If its not too much trouble, I’d like to ask a follow-up. You mentioned the ability to capture multiple groups. This is intriguing, and I’m wondering if its possible to replace multiple groups containing several strings with a particular prefix and then replace such that the prefix is inserted into a container, followed by the string which is connected to that specific prefix.

Examples of strings:

[prefix]_[string]

amb_cold.ogg
amb_lava.ogg

wat_boom.ogg
wat_fire.ogg

wind_lt.ogg
wind_hv.ogg

Example of container (one for all):

FMODSoundAdd("sounds\chars\[prefix]\[prefix]_[string]",0,0);

Desired replace results:

FMODSoundAdd("sounds\chars\amb\amb_cold.ogg",0,0);
FMODSoundAdd("sounds\chars\amb\amb_lava.ogg",0,0);
FMODSoundAdd("sounds\chars\wat\wat_boom.ogg",0,0);
FMODSoundAdd("sounds\chars\wat\wat_fire.ogg",0,0);
FMODSoundAdd("sounds\chars\wind\wind_lt.ogg",0,0);
FMODSoundAdd("sounds\chars\wind\wind_hv.ogg",0,0);

With the current method, the replacement will need to be done for all prefix groups. I know this is a long tangent, but just wondering if its possible to do this. I’ll try to achieve this myself if its possible, but will more than likely end up getting stuck.

Thanks again.

0 Likes

#4

Find pattern : (\w+?)_(\w+\.\w+)
Replace pattern : MODSoundAdd("sounds\\chars\\\1\\\1_\2",0,0);

To explain a bit: (\w+?)_ just means capture any word character until the first _, without it , in the case of amb_cold_1.ogg, the first capture would have amb_cold instead if amb

But you should read some tutorial about regexp, it is always very handy : https://www.regular-expressions.info/tutorial.html

1 Like

#5

Thank you once again Clams.

The solution works great. I have bookmarked that tutorial link you’ve provided and will go through it to help solve similar issues in the future.

0 Likes