批处理文件替换感叹号与逃感叹号 [英] Batch File Replace Exclamation Mark with Escaped Exclamation Mark

查看:831
本文介绍了批处理文件替换感叹号与逃感叹号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个批处理文件,得到的数据作为一个变量,并在该变量它有一个感叹号。

I am working on a batch file that get's data as a variable, and in that variable it has an exclamation mark.

我所试图做的是必要的转义字符添加到变量。

What i am trying to do is add the necessary escape characters to the variable.

"Title":"Turk 182!"

以上是我与工作中的数据的一个例子。

Above is an example of the data I am working with.

setlocal EnableDelayedExpansion
rem replace ! with ^^!
set var=%var:!=^^!!%

但我不知道这是正确的语法,因为我怎么能逃脱esclamation标志,如果它也被用来作为搜索的一部分,并更换?

But I am not sure that is the correct syntax, since how can i escape an esclamation mark if it's also used as part of the search and replace?

我想与逃脱的版本替换所有esclamation大关,因此它可以被显示,并与合作。

I am trying to replace all esclamation mark with an escaped version so it can be displayed and worked with.

顺便说一句,有没有批任何现有的功能,将删除,并允许所有特殊字符转义?

Btw, is there any existing functions in batch that will remove and allow escaping of all special characters?

下面是更多code可能有助于解释我在做什么。

Here is more code that may help explain what I am doing.

{"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变量在循环,它错误的出,而不是真正的价值我只看到实际的替换命令我上面使用。

Then i place the json variable in the loop and it error's out instead of the true value I just see the actual replace command i used above.

推荐答案

我不相信你真的需要躲避。这是可以做到的,但是杰布试图解释,它可以变得复杂了。

I'm not convinced you really need to escape !. It is possible to do, but as jeb tried to explain, it can get complicated.

根据您所提供的code,它看起来像你对我有什么不明白推迟扩张一样。我建议你​​键入 HELP SET 从命令提示符和阅读文档。延迟扩展的描述开始大约一半的方式来与读取的最后,对于延迟环境变量扩充的支持......

Based on the code you provided, it looks to me like you do not understand what delayed expansion does. I suggest you type HELP SET from a command prompt and read the documentation. The description of delayed expansion begins about half way down with the line that reads "Finally, support for delayed environment variable expansion..."

有延迟扩展等主要优点。其中一个最大的优点就是你永远不用担心当您使用延迟扩展转义任何特殊字符。而使用正常的扩张转义字符,是一种痛苦,直到你获得的经验可以是非常混乱。 (这真的是合乎逻辑和predictable,但直到你了解它,它看起来像废话)

There are other major benefits to delayed expansion. One of the biggest advantages is you never have to worry about escaping any special characters when you use delayed expansion. Escaping characters while using normal expansion is a pain, and can be extremely confusing until you gain experience. (It really is logical and predictable, but until you understand it, it looks like gibberish)

延迟扩张的最大问题是它不与FOR循环时,数据中包含发挥好。这是因为FOR变量扩大后发生延迟的扩张,使含值将被破坏。在下面我JSON解析器,我切换延迟关闭扩张内循环,以避免这样的问题。

The biggest problem with delayed expansion is it does not play nice with FOR loops when the data contains !. That is because the delayed expansion occurs after the FOR variable is expanded, so values containing ! will be corrupted. In my json parser below, I toggle the delayed expansion off within the loop to avoid that problem.

我不知道很多关于JSON,所以下面我的解决方案可能很幼稚(不完全统计)。但这里是一个简单的JSON解析器与您提供的数据工作。我把你的JSON字符串在一个名为test.txt的

I don't know much about json, so my solution below may be naive (incomplete). But here is a simple json parser that works with the data you provided. I put your json string in a file named "test.txt"

@echo off
setlocal disableDelayedExpansion
setlocal enableDelayedExpansion

::Read the json string from a file
<test.txt set /p "json="

::Define LF variable to contain a linefeed
set LF=^


::The above 2 blank lines are critical - DO NOT REMOVE

::Strip the enclosing braces
set "json=!json:~1,-1!"

::Substitute a linefeed for ","
for %%A in ("!LF!") do set "json=!json:","=%%~A!"

::Substitute = for ":"
set "json=!json:":"==!"

::Remove remaining "
set "json=!json:"=!"

::Loop through the data, creating variables of the form var_name=value
for /f "delims=" %%A in ("!json!") do (

  REM If delayed expansion is enabled then endlocal to get back to disabled state
  if "!!" equ "" endlocal

  REM Create the variable
  set "var_%%A"
)

::Display the results - list all variables that begin with var_
set var_

下面是输出

var_Actors=Timothy Hutton, Robert Urich, Kim Cattrall, Robert Culp
var_Director=Bob Clark
var_Genre=Action, Comedy, Drama
var_imdbID=tt0090217
var_imdbRating=5.7
var_imdbVotes=2,360
var_Plot=Jimmy Lynch is angry because his older brother, who was injured as a result of an off duty fire rescue...
var_Poster=http://ia.media-imdb.com/images/M/MV5BMTQ2OTk1ODA1MV5BMl5BanBnXkFtZTcwNjYwNjgyMQ@@._V1_SX300.jpg
var_Rated=PG-13
var_Released=15 Feb 1985
var_Response=True
var_Runtime=1 h 42 min
var_Title=Turk 182!
var_Writer=Denis Hamill, James Gregory Kingston
var_Year=1985

这篇关于批处理文件替换感叹号与逃感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆