Batch file 如何在字符串替换窗口命令中跳过特殊字符(=)

Batch file 如何在字符串替换窗口命令中跳过特殊字符(=),batch-file,cmd,Batch File,Cmd,我们可以跳过/转义字符串替换中的等号(=)吗? 我希望输出为“abcde”,但它会打印“a=b” 有什么建议吗?这里有一个笨拙的方法: @echo off :: the string that contains equals set "stringWithEquals===blah==blah==blah=" ::the string you want to replace with set replace_with=X1 echo %stringWithEquals% call :eqre

我们可以跳过/转义字符串替换中的等号(=)吗? 我希望输出为“abcde”,但它会打印“a=b”

有什么建议吗?

这里有一个笨拙的方法:

@echo off

:: the string that contains equals
set "stringWithEquals===blah==blah==blah="
::the string you want to replace with
set replace_with=X1
echo %stringWithEquals%

call :eqrepl "%stringWithEquals%" %replace_with% res
::the result
echo %res%
goto :eof


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: here are the functions you need
::
:eqrepl String Replacer [RtnVar]
setlocal
rem  the result of the operation will be stored here
set "result=#%~1#"
set "replacer=%~2"
call :strlen0 result wl
call :strlen0 replacer rl

:start

  set "part1="
  set "part2="

  rem splitting the string on two parts
  for /f "tokens=1* delims==" %%w in ("%result%") do (
   set "part1=%%w"
   set "part2=%%x"
  )

  rem calculating the count replace strings we should use
  call :strlen0 part1 p1l
  call :strlen0 part2 p2l
  set /a iteration_end=wl-p1l-p2l

  rem creating a sequence with replaced strings
  setlocal enableDelayedExpansion
  set "sequence="
  for /l %%i in (1,1,%iteration_end%) do (
   set sequence=!sequence!%replacer%
  )
  endlocal & set "sequence=%sequence%"

  rem adjust the string length
  set /a wl=wl+iteration_end*(rl-1)

  rem replacing for the current iteration
  set result=%part1%%sequence%%part2%
  rem if the second part is empty the task is over
  if "%part2%" equ "" (
   set result=%result:~1,-1%
   goto :endloop
  )


  goto :start

:endloop
endlocal & if "%~3" neq "" (set %~3=%result%) else echo %result%
exit /b

:strlen0  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!s:~%%N,1!" neq "" (
      set /a "len+=%%N"
      set "s=!s:~%%N!"
    )
  )
  endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b
这里有一个笨拙的方法:

@echo off

:: the string that contains equals
set "stringWithEquals===blah==blah==blah="
::the string you want to replace with
set replace_with=X1
echo %stringWithEquals%

call :eqrepl "%stringWithEquals%" %replace_with% res
::the result
echo %res%
goto :eof


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: here are the functions you need
::
:eqrepl String Replacer [RtnVar]
setlocal
rem  the result of the operation will be stored here
set "result=#%~1#"
set "replacer=%~2"
call :strlen0 result wl
call :strlen0 replacer rl

:start

  set "part1="
  set "part2="

  rem splitting the string on two parts
  for /f "tokens=1* delims==" %%w in ("%result%") do (
   set "part1=%%w"
   set "part2=%%x"
  )

  rem calculating the count replace strings we should use
  call :strlen0 part1 p1l
  call :strlen0 part2 p2l
  set /a iteration_end=wl-p1l-p2l

  rem creating a sequence with replaced strings
  setlocal enableDelayedExpansion
  set "sequence="
  for /l %%i in (1,1,%iteration_end%) do (
   set sequence=!sequence!%replacer%
  )
  endlocal & set "sequence=%sequence%"

  rem adjust the string length
  set /a wl=wl+iteration_end*(rl-1)

  rem replacing for the current iteration
  set result=%part1%%sequence%%part2%
  rem if the second part is empty the task is over
  if "%part2%" equ "" (
   set result=%result:~1,-1%
   goto :endloop
  )


  goto :start

:endloop
endlocal & if "%~3" neq "" (set %~3=%result%) else echo %result%
exit /b

:strlen0  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!s:~%%N,1!" neq "" (
      set /a "len+=%%N"
      set "s=!s:~%%N!"
    )
  )
  endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b

=
在子字符串替换语法中将搜索字符串与替换字符串分开,因此不能使用它;您可以通过编写自己的批处理脚本或借用其他语言(如PowerShell、JavaScript、VBScript等)来进行替换,
=
的可能重复项在子字符串替换语法中将搜索字符串与替换字符串分开,因此您无法使用它;您可以通过编写自己的批处理脚本或借用另一种语言(如PowerShell、JavaScript、VBScript)来进行替换,这些语言可能与