使用 FINDSTR 在批处理文件中转义反斜杠
在我的 svn Pre commit hooks 中,我使用 findstr 来阻止提交某些文件类型。 我现在想将其扩展到目录,首先是 \obj\ 目录,但是我在正则表达式和转义目录的 \ 方面遇到问题
目前我已经
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ".obj\\\"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
exit 0
尝试过仅在末尾使用 \ 但似乎逃脱了双引号也是如此?
有任何想法吗?
In my svn Pre commit hooks I use findstr to block certain file types beign committed.
I now want to extend this to directories, in the first instance \obj\ directories however I am having problems with the Regular expression and escaping the \ of the dir
Currently I have
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ".obj\\\"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
exit 0
i have tried with just \ at the end but that seems to escape the double quotes as well?
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据经验,以下任一命令都可以满足您的要求:
由于您指定了
/R
,因此您还需要在.
之前添加反斜杠,否则它会被解释为通配符。旁注:从我的测试来看,findstr.exe 使用了 MS 的 C 库所使用的有点奇怪的引用规则,如 Microsoft 网站。 在这种特殊情况下,相关规则提到,前面有偶数个反斜杠的双引号字符被解释为反斜杠数的一半。 (是的,这很奇怪,而且当您意识到 cmd.exe 也特别对待双引号字符时,情况会变得更奇怪......坦率地说,在 Windows 上正确引用事物是一个痛苦的世界。)
Empirically, either of the following commands does what you want:
Since you specified
/R
, you also need a backslash before the.
because it will otherwise be interpreted as a wildcard character.Side note: it appears from my tests that findstr.exe uses the somewhat strange quoting rules used by MS's C library, described on Microsoft's website. In this particular case the relevant rule is the one mentioning that a double-quote character preceded by an even number of backslashes is interpreted to be half as many backslashes. (Yes, it's weird, and it gets weirder when you realise that cmd.exe treats double-quote characters specially too... Quoting things properly on Windows is a world of pain, frankly.)
在正则表达式中,反斜杠应该双转义才能在字符串正则表达式中正确解释:
但在您的情况下,因为您的正则表达式应该匹配
.obj
文件和“obj
”目录,我建议:因为您的原始正则表达式(“
.obj\\
”)只会检测到“.obj
”目录,而不是“obj”。 因此,“
?
”自“.”起 表示任何字符,您还需要在其前面添加“
\\
”来更改其解释。In a regexp, backslash should be double escaped to be correctly interpreted in a String regex:
But in your case, since your regex should match both
.obj
files and "obj
" directories, I would suggest:because your orignal regex ("
.obj\\
") would only have detected ".obj
" directory, not "obj
". Hence the '?
'Since '.' means any character, you also need "
\\
" before it to change its interpretation.您遇到的错误是什么?
这可能是一个转移注意力的话题,但 SVN 使用
/
作为路径分隔符,这会在 Windows 下导致一些问题。 我必须将以下内容放入所有挂钩脚本中,以将/
更改为\
:What's the error you're getting?
This may be a red herring, but SVN uses
/
as the path seperator, which causes some problems under Windows. I had to put the following in all my hook scripts to change/
to\
:我使用以下方法解决了这个问题。
I solved this using the following.
在这种情况下你真的需要正则表达式吗? 如果您只是搜索子字符串“\obj\”,您可以使用 /C 而不是 /R 将文本视为文字匹配字符串:
Do you actually need a regular expression in this case? If you're just searching for the substring "\obj\" you can use /C instead of /R to treat the text as a literal match string:
如果 findstr 由两个字符序列
\"
组成,则搜索字符串必须为\\\\"
(四个反斜杠)。In the case of findstr consisting of the two character sequence
\"
the search string must be\\\\"
(four backslashes).