批量字符转义 [英] Batch character escaping

查看:165
本文介绍了批量字符转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写批处理脚本为Windows精通相当,但让我为难,即使在所有这些年来如何正确地转义字符。这是特别困难的时候试图弄清楚正确的方式来逃避使用的常规前pression使用sed。是否有任何工具,可以帮助我吗?或许是,让我在一个正常的字符串粘贴,它吐出该字符串的正确转义版本?

更新:我不愿意举一个例子,因为我不是在寻找如何逃脱一个特定字符串的答案。我也不是在寻找一个解决方案,将一个特定的应用工作。我正在寻找一种工具,将帮助我得到转义语法正确的每一个我永远需要,不管什么工具可以通过命令行来消费它逃脱字符串。

话虽这么说,正则表达式我真正想要的是

 (^ *)(表格产品=)([^] *)FormType =[^] *FormID =([0-9] [ 0-9] *)。* $

以真正的正则表达式(即转义尽可能批次而言),在一些SED语法把它包起来,如 ssedS @ ---将逃脱正则表达式在这里--- @http:/ / PSPH / \\ 1 / \\ 2 @ G%〜1终于逃脱它。再次,有没有可以帮助逃脱任何字符串批处理命令行中使用任何工具?

P.S。有这么多例外批量的转义语法,我甚至会满足于一个良好的小抄。


解决方案

这是适应从网页。批处理文件 - 转义字符在罗布·范德Woude的脚本页网站

TLDR

的Windows(和DOS)批处理文件字符转义复杂


  

    

就像宇宙中,如果有谁确实的完全明白过来批则语言将瞬间被自己的一个无限离奇更复杂的版本替换。这显然​​已经发生了至少一次前;)


  

百分号

可转义为 %% - 可能并不总是需要[转义] doublequoted在弦,只是尝试

通常情况下,用符号

这些字符可能不总是被要求[转义]在doublequoted字符串,但它不会伤害


  • ^

  • &安培;

  • <

  • >

  • |

是应当[转义]只有在 FOR / F 主题(即间括号),除非 backq 使用

`是应当[转义]只有在 FOR / F 主题(即间括号),如果 backq 使用

这些字符要求[转义]只有在 FOR / F 主题(即括号之间),均匀在doublequoted字符串



  • ;

  • =



双逃生惊叹号使用延迟的变量扩充时

必须进行转义 ^^!时延迟的变量扩充是有效的。

双双引号中的找到搜索模式

FINDSTR

使用反斜杠正则表达式模式


  • \\

  • [

  • ]



  • *


另外

罗布(与自己通过电子邮件通信)进一步评论这个问题:


  

至于答案,恐怕混沌甚至不如原来的海报实现:转义括号的要求也依赖于字符串code座内正在或不


  
  

我想每一个字符前一个自动化的工具可能只需插入插入符号,那么所有翻倍百分号 - 如果字符串doublequoted它仍然会失败


此外,个别项目负责解析他们的命令行参数,以便一些需​​要转义为,例如为 SED ssed ,可能是由于所谓的批处理脚本的具体方案。

I'm fairly proficient at writing Batch scripts for Windows, but even after all these years how to correctly escape characters puzzles me. It's especially difficult when trying figure out the correct way to escape a regular expression for use with sed. Is there any tool that can help me? Perhaps something that allows me to paste in a "normal" string and it spits out the correctly escaped version of that string?

Update: I'm reluctant to give an example because I'm not looking for an answer on how to escape one specific string. I'm also not looking for a solution that will work for one specific app. I'm looking for a tool that will help me get the escape syntax correct for every string I ever need to escape no matter what tool might be consuming it from the command line.

That being said the regex I really want is

(^.*)(Form Product=")([^"]*") FormType="[^"]*" FormID="([0-9][0-9]*)".*$

Take that true regex (i.e. unescaped as far as BATCH is concerned) and wrap it in some sed syntax such as ssed "s@ --- Insert escaped regex here --- @http://psph/\1/\2@g" "%~1" and finally escape it... Again, is there any tool that can assist in escaping any string for use on the BATCH command line?

p.s. There are so many exceptions to BATCH's escaping syntax that I'll even settle for a good cheat sheet.

解决方案

This is adapted with permission of the author from the page Batch files - Escape Characters on Rob van der Woude's Scripting Pages site.

TLDR

Windows (and DOS) batch file character escaping is complicated:

Much like the universe, if anyone ever does fully come to understand Batch then the language will instantly be replaced by an infinitely weirder and more complex version of itself. This has obviously happened at least once before ;)

Percent Sign

% can be escaped as %% – "May not always be required [to be escaped] in doublequoted strings, just try"

Generally, Use a Caret

These characters "may not always be required [to be escaped] in doublequoted strings, but it won't hurt":

  • ^
  • &
  • <
  • >
  • |

' is "required [to be escaped] only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used"

` is "required [to be escaped] only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used"

These characters are "required [to be escaped] only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings":

  • ,
  • ;
  • =
  • (
  • )

Double Escape Exclamation Points when Using Delayed Variable Expansion

! must be escaped ^^! when delayed variable expansion is active.

Double Double-Quotes in find Search Patterns

"""

Use a Backslash in findstr Regex Patterns

  • \
  • [
  • ]
  • "
  • .
  • *
  • ?

Also

Rob commented further on this question (via email correspondence with myself):

As for the answer, I'm afraid the chaos is even worse than the original poster realizes: requirements for escaping parentheses also depend on the string being inside a code block or not!

I guess an automated tool could just insert a caret before every character, then doubling all percent signs - and it would still fail if the string is doublequoted!

Further, individual programs are responsible for parsing their command line arguments so some of the escaping required for, e.g. for sed or ssed, may be due to the specific programs called in the batch scripts.

这篇关于批量字符转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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