Special Characters contained in Variable used in String:Replace command...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Arianax
Posts: 2
Joined: 06 Apr 2020 14:38

Special Characters contained in Variable used in String:Replace command...

#1 Post by Arianax » 06 Apr 2020 15:19

Hi all,

Got a tough one for you here. Tried asking this on many other forums but no real answer yet, just workarounds...

Code: Select all

SET String=Some stuff. Desc=Some more stuff. Some final stuff.
SET Rep=CHANGED

SET Modify=Desc=Some more stuff.

SET "New=!String:%Modify%=%Rep%!"

ECHO !New!
The problem with the above code is that it interprets the '=' symbol as part of the Find:Replace so we get the following output for !New!:

Code: Select all

Some stuff.Some more stuff. =CHANGED=Some more stuff. Some final stuff.
How can I change the syntax of the following command:

Code: Select all

SET "New=!String:%Modify%=%Rep%!"
So the '=' contained in the variable %Modify% isn't interpreted as part of the above command?

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Special Characters contained in Variable used in String:Replace command...

#2 Post by jeb » 07 Apr 2020 00:00

Hi Arianax,

simple: You can't :!:

It's not possible to escape an equal sign in the search string.

There exist some pure batch solutions (also on Dostips), but I would recommend the easy to use and powerful solution from Dave JREPL.

This is a JScript/Batch hybrid from one of our honored, expert members.

If you still insist on using a pure batch solution, you can search this forum (probably tougher than writing your own solution).

One pseudo code solution could be:
Build a modified_string
1. Replace all hash signs with #H in your string
2. Replace all equal signs with #E in your string

Obviously the second point is problematic, too, but can be solved by
2b. Loop over all characters of the string and check if it is an equal sign


3. Do the same for your search string
4. Execute the replace expression result=!modified_string:modified_search=repalce!
5. Replace remaining #E back to equal signs by: result=!result:#E==!
6. Replace remaining #H back to equal signs by: result=!result:#H=#!

jeb

Post Reply