我正在处理一个将数据作为变量获取的批处理文件,并且在该变量中它有一个感叹号。
我想要做的是将必要的转义字符添加到变量中。
"Title":"Turk 182!"
以上是我正在使用的数据的示例。
setlocal EnableDelayedExpansion
rem replace ! with ^^!
set var=%var:!=^^!!%
但我不确定这是正确的语法,因为如果它也被用作搜索和替换的一部分,我怎么能逃避一个感叹号?
我正在尝试用转义版本替换所有 esclamation 标记,以便可以显示和使用它。
顺便说一句,批处理中是否有任何现有功能可以删除并允许转义所有特殊字符?
这里有更多代码可以帮助解释我在做什么。
{"Title":"Turk 182!","Year":"1985","Rated":"PG-13","Released":"15 Feb 1985","Runtime":"1 h 42 min","Genre":"Action, Comedy, Drama","Director":"Bob Clark","Writer":"Denis Hamill, James Gregory Kingston","Actors":"Timothy Hutton, Robert Urich, Kim Cattrall, Robert Culp","Plot":"Jimmy Lynch is angry because his older brother, who was injured as a result of an off duty fire rescue...","Poster":"http://ia.media-imdb.com/images/M/MV5BMTQ2OTk1ODA1MV5BMl5BanBnXkFtZTcwNjYwNjgyMQ@@._V1_SX300.jpg","imdbRating":"5.7","imdbVotes":"2,360","imdbID":"tt0090217","Response":"True"}
rem removes starting and ending brackets
set json=%json:~1,-1%
setlocal EnableDelayedExpansion
rem replace "," with linebreak
set json=!json:","="#"!
setlocal EnableDelayedExpansion
rem replace ! with ^^!
set json=%json:!=^^!!%
setlocal DisableDelayedExpansion
echo %json%
echo.
exit /b
然后我将 json 变量放在循环中,它错误而不是真实值,我只看到我上面使用的实际替换命令。