1.

Solve : String Replace with Wildcard?

Answer»

This question is regarding modifying text files
I need to replace a constant string followed by a variable string with a dynamic string like so:

Code: [Select]Constant::Hello   -> Constant::English
Constant::Bonjour -> Constant::French
To the best of my knowledge, using a wildcard is the way to locate the string within the file:

Code: [Select]Constant::*
but this doesn't seem to work with the replace macro (for the lack of the right name) I'm using in my bat:

Code: [Select]SET str=!Constant::*=Constant::%language%!
I've also tried setting the new and old strings as variables:

Code: [Select]set old = Constant::*
set new = Constant::%language%

// loops

set str=!%old%=%new%!
I urgently need to find a way to do this, preferably using the macro (or whatever) I'm already using

I don't really use DOS, i'm used to coding in C++ and it's takes me a while to get used to the way variables are used.
I find it rather hard to read, so please comment your suggestions for ease of reading.
I'd like to avoid using non-standard FUNCTIONS if possible

Thanks
Could you explain a bit more clearly what you are trying to do, and what you mean by the word "macro"?

You can replace a literal string with another like this

set variable=%variable:search=replace%

to replace father with mother:

set string1=hello::father

set string1=%string1:father=mother%

To use a variable, DOUBLE the percent signs around the replace variable

set string1=hello::mother
set string2=father
echo %string1:mother=%%string2%%

(result: hello::father)







'macro' is just the best name I could think of for the '%string1:father=mother%' bit.

I know that line replaces a literal string, but I need to find a literal string followed by a variable (wildcard) string.

A wildcard needs to be found (like 'Content::*' where * is a wildcard)
This script loops through lots of files which contain something like:

Content::Hello
Content::Bonjour
Content::Goodbye
Content::Ciao
Content::AuRevoir

Which need to be replaced with:

Content::English
Content::French
Content::English
Content::Italian
Content::French

The fact it's languages and how the language is determined doesn't matter, it's just an example.

I managed to get it replacing with a variable, but to clarify what you've said, is it:

echo %string1:mother=%%string2%%

or

echo %string1:mother=%%string2%%%

because in the end I used a %%a (character?) variable rather than a %string% variable. I may be misunderstanding the significance of some of the %.
Also i'm using Delayed Expansion, so my line looks like this:

set str=!str:Content::=Content::%%a_!

(the '_' is because it's inserting before rather than replacing the original string, so 'Content::Hello' becomes 'Content::English_Hello')

Ultimately I want to be left with 'Content::English' rather than 'Content::English_Hello' but having a better understanding of what the % are for would be useful too.

I hope that clarifies everything, as I need to solve this asap.
 You can check this out and possibly solve your issues.

Also, it may be easier to set the for token to a named variable earlier in the loop to avoid issues and confusion. I.e.

set VAR1=%%a

then

call set str=!str:Content::=Content::%%var1%%!



Discussion

No Comment Found