Page 1 of 1

syntax to replace a search string containing =

Posted: 02 Jun 2013 10:43
by sambasiva
Hi Batch Gurus,


String="Hi how is the day=100 for you"

My requirement is to replace day=100 with day=222 in the above string.


I am not able to figure out the syntax of the same.

Could someone please help me with this...

Thanks in Advance,
Sambasiva

Re: syntax to replace a search string containing =

Posted: 02 Jun 2013 14:07
by Squashman
Why are you trying to replace the word day and the equals sign with the same thing? Just replace the 100 with 222.

Re: syntax to replace a search string containing =

Posted: 02 Jun 2013 20:19
by foxidrive
Here you go:

Code: Select all

@echo off
set "String=Hi how is the day=100 for you"

call :Search_and_replace "%string%" "(.*day=)[0-9]*(.*)" "$1222$2"

for /f "delims=" %%c in ('cscript /nologo _.vbs') do set "output=%%c"
)

echo "%output%"

del _.vbs
goto :EOF

:Search_and_replace
set s=regex.replace("%~1","%~3")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=True
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~2"
>>_.vbs echo wscript.stdOut.write %s%

Re: syntax to replace a search string containing =

Posted: 02 Jun 2013 21:27
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "String=Hi how is the day=100 for you"

REM My requirement is to replace day=100 with day=222 in the above string.
set old=day=100
set new=day=222
for /F "tokens=1,2 delims==" %%a in ("%String%") do (
   rem Char next to %%a in line below is Ascii-255, not space
   set "left=%%a "
   set "right=%%b"
)
for /F "tokens=1,2 delims==" %%a in ("%old%") do (
   for /F "tokens=1,2 delims==" %%x in ("%new%") do (
      rem Char next to %%a in line below is Ascii-255, not space
      set "String=!left:%%a =%%x=!!right:*%%b=%%y!"
   )
)
echo %String%


Antonio

Re: syntax to replace a search string containing =

Posted: 02 Jun 2013 22:53
by sambasiva
Both the solutions worked...
Thanks alot for the quick help...

Re: syntax to replace a search string containing =

Posted: 03 Jun 2013 02:24
by Endoro
Aacini wrote:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "String=Hi how is the day=100 for you"

REM My requirement is to replace day=100 with day=222 in the above string.
set old=day=100
set new=day=222
for /F "tokens=1,2 delims==" %%a in ("%String%") do (
   rem Char next to %%a in line below is Ascii-255, not space
   set "left=%%a "
   set "right=%%b"
)
for /F "tokens=1,2 delims==" %%a in ("%old%") do (
   for /F "tokens=1,2 delims==" %%x in ("%new%") do (
      rem Char next to %%a in line below is Ascii-255, not space
      set "String=!left:%%a =%%x=!!right:*%%b=%%y!"
   )
)
echo %String%
Very nice :) !

Re: syntax to replace a search string containing =

Posted: 03 Jun 2013 07:30
by Ocalabob
String="Hi how is the day=100 for you"
My requirement is to replace day=100 with day=222 in the above string.

Code: Select all

@echo off
Echo "Hi how is the day=100 for you" |SED "s/day=100/day=222/"
Pause>nul

Find and Replace is a 'major food group' for SED. :)