在CMD替换中,如何删除从特定字符开始到结尾的子字符串
In CMD substitution, how to remove substring which starts from specific character to the end
从原始字符串中替换或删除子字符串,通常在windows CMD中使用这种形式。
set result=%original:<strings_to_be_removed>=<strngs_to_be_newly_substituted>%
所以它适用于以下许多情况..
set "original=Questions that may already have your answer"
set "you=%original:that=you%"
set you
you=Questions you may already have your answer
set challenge=%original:Questions=Challenges%
set challenge
challenge=Challenges that may already have your answer
set answer=%original:*your=%
set answer
answer= answer
但我不知道如何替换或删除从特定字符(或单词)开始到原始字符串结尾的子字符串。
例如,假设我想删除从 "that" 开始到原始字符串结尾的子字符串。然后我按如下方式使用命令并期望结果字符串为 "Questions "
set result=%original:that*=%
但是,结果字符串与原始字符串没有区别。没有效果发生。替换意图失败..
set result
result=Questions that may already have your answer
我在这种情况下使用了转义字符'^'、'\',但没有效果..
如何解决此问题以替换或删除此类子字符串?
如何替换或删除从特定字符(或单词)开始到原始字符串结尾的子字符串?谢谢:-)
你可以欺骗命令行解析器来做到这一点:
set "original=Questions that may already have your answer"
set result=%original: may =&REM %
set result
遗憾的是,set "result=%original:may=&REM %"
不起作用,因此该字符串应该没有毒字符。
工作原理:
将单词替换为 &REM
,这使得您的字符串:
Questions that & REM already have your answer
和命令:
set result=Questions that & REM already have your answer
&
用作命令的分隔符(尝试 echo hello&echo world
,它执行两个 echo 命令)。所以真正执行的是两个命令:
set result=Questions that
和
REM already have your answer
它也不适用于延迟扩展。您可以改用子函数:
setlocal enabledelayedexpansion
if 1==1 (
set "original=Questions that may already have your answer"
call :substring "!original!"
set result
)
goto :eof
:substring
set org=%~1
set result=%org: may =&REM %
goto :eof
从原始字符串中替换或删除子字符串,通常在windows CMD中使用这种形式。
set result=%original:<strings_to_be_removed>=<strngs_to_be_newly_substituted>%
所以它适用于以下许多情况..
set "original=Questions that may already have your answer"
set "you=%original:that=you%"
set you
you=Questions you may already have your answer
set challenge=%original:Questions=Challenges%
set challenge
challenge=Challenges that may already have your answer
set answer=%original:*your=%
set answer
answer= answer
但我不知道如何替换或删除从特定字符(或单词)开始到原始字符串结尾的子字符串。
例如,假设我想删除从 "that" 开始到原始字符串结尾的子字符串。然后我按如下方式使用命令并期望结果字符串为 "Questions "
set result=%original:that*=%
但是,结果字符串与原始字符串没有区别。没有效果发生。替换意图失败..
set result
result=Questions that may already have your answer
我在这种情况下使用了转义字符'^'、'\',但没有效果..
如何解决此问题以替换或删除此类子字符串?
如何替换或删除从特定字符(或单词)开始到原始字符串结尾的子字符串?谢谢:-)
你可以欺骗命令行解析器来做到这一点:
set "original=Questions that may already have your answer"
set result=%original: may =&REM %
set result
遗憾的是,set "result=%original:may=&REM %"
不起作用,因此该字符串应该没有毒字符。
工作原理:
将单词替换为 &REM
,这使得您的字符串:
Questions that & REM already have your answer
和命令:
set result=Questions that & REM already have your answer
&
用作命令的分隔符(尝试 echo hello&echo world
,它执行两个 echo 命令)。所以真正执行的是两个命令:
set result=Questions that
和
REM already have your answer
它也不适用于延迟扩展。您可以改用子函数:
setlocal enabledelayedexpansion
if 1==1 (
set "original=Questions that may already have your answer"
call :substring "!original!"
set result
)
goto :eof
:substring
set org=%~1
set result=%org: may =&REM %
goto :eof