Windows 批处理检查变量是否以特定字符串开头、结尾并包含特定字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15707742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Batch check if variable starts with, ends with and contains a specific string
提问by BrainStone
I'm trying to check if a variable in a batch file starts with "
contains BETA
somewhere and ends with ")
.
我正在尝试检查批处理文件中的变量是否以"
contains开头BETA
并以")
.
Is it possible? And if yes, may somebody help me with that?
是否可以?如果是的话,有人可以帮我吗?
采纳答案by Magoo
@ECHO OFF
SETLOCAL
SET var=abc&CALL :check
SET var="abc"&CALL :llcheck
SET var="")&CALL :check
SET var=")"&CALL :llcheck
SET var=abc")"&CALL :llcheck
SET var=xyzbetazyx&CALL :check
SET var="xyzbetazyx"&CALL :llcheck
SET var=xyzbetazyx")"&CALL :llcheck
SET var=xyzbetazyx")"&CALL :check
SET var="xyzbetazyx")&CALL :check
GOTO :eof
:: Lop last, then check
:llcheck
SET var=%var:~0,-1%
:check
SET result=N
SET var2=%var%
SET varvar=%var%
>test1.txt ECHO %var:~0,1%%var:~-2%
>test2.txt ECHO "")
FC test1.txt test2.txt >nul
IF ERRORLEVEL 1 GOTO done
SET var|FINDSTR /b "var="|FINDSTR /i "beta" >nul
IF ERRORLEVEL 1 GOTO done
SET result=Y
:done
ECHO %var% starts " has BETA and ends ") : %result%
GOTO :eof
Where there's a will...
有意愿的地方...
Setting variables with unbalanced "
can be tricky. I just set it up balanced and lopped off the last character (:llcheck
entry : lop last and check.
设置不平衡的变量"
可能很棘手。我只是将它设置为平衡并删除了最后一个字符(:llcheck
条目:lop last 并检查。
Essentiall, I've copied the variable into var2
and varvar
in oder to demo what happens should these variablenames be set.
本质上,我已经将变量复制到var2
并varvar
在 oder 中演示了如果设置这些变量名会发生什么。
Two files are then generated. TEST1.TXT
contains the first and last 2 character of var
and TEST2.TXT
simply "")
然后生成两个文件。TEST1.TXT
包含的第一个和最后2个字符var
,并TEST2.TXT
简单地"")
Compare the two - if they're not identical, then the variable does NOT start "
and end ")
Otherwise well - could simply have written var
out to a file and used findstr
to find beta
, but I decided to send the output of SET var
which should be the contents of ALL
of the var*
variables in the form
比较两个-如果他们不相同,则该变量不启动"
和结束")
,否则好-可以简单地写var
了一个文件,并使用findstr
找到beta
,但我决定派的输出SET var
应该是内容ALL
的var*
表格中的变量
var=abc
var2=abc
varvar=abc
into findstr, finding the one that starts (/b
) var=
and finding whether THATcontains the string beta
The /i
selects case-insensitive. If you want specifically BETA
in UPPER-CASE, simply change beta
to BETA
and remove the /i
.
进入 findstr,查找以 ( /b
)开头的那个,var=
并查找THAT是否包含字符串beta
The /i
selects 不区分大小写。如果你特别希望BETA
在大写,只需更改beta
到BETA
并删除/i
。
result
is set to Y
or N
result
设置为Y
或N
回答by dbenham
Assuming your variable does not contain any line feeds or carriage returns, then all that is needed is a single FINDSTR command. It has limited regular expression support that is more than enough for this problem.
假设您的变量不包含任何换行符或回车符,那么只需要一个 FINDSTR 命令即可。它具有有限的正则表达式支持,足以解决这个问题。
@echo off
setlocal
set test1=abc
set test2="abc"
set test3="")
set test4=")"
set test5=abc")"
set test6=xyzBETAzyx
set test7="xyz BETA zyx"
set test8=xyzBETAzyx")"
set test9=xyzBETAzyx")"
set test10="xyzbetazyx")
set test11="xyzBETAzyx")
for /l %%N in (1 1 11) do call :test test%%N
exit /b
:test variableName
setlocal enableDelayedExpansion
echo !%1!|>nul findstr /rx \".*BETA.*\") && set "result=PASS" || set "result=FAIL"
echo %1 = !result!: !%1!
exit /b
Only TEST11 passes.
只有 TEST11 通过。
The /R
option forces the search string to be treated as a regular expression.
该/R
选项强制将搜索字符串视为正则表达式。
The /X
option means it must be an exact match - the entire string must match the search string.
该/X
选项意味着它必须完全匹配 - 整个字符串必须与搜索字符串匹配。
.*
is the regex wildcard expression that will match any string of characters.
.*
是将匹配任何字符串的正则表达式通配符表达式。
I redirect the FINDSTR output to NUL because we don't need to see the line. We just want the return code
我将 FINDSTR 输出重定向到 NUL,因为我们不需要看到该行。我们只想要返回码
FINDSTR requires double quote literals to be escaped as \"
FINDSTR 要求将双引号文字转义为 \"
Use delayed expansion so that you can safely echo any value within the variable.
使用延迟扩展,以便您可以安全地回显变量中的任何值。
I use &&
to conditionally take action when FINDSTR succeeded, and ||
to conditionally take action when FINDSTR failed.
我&&
习惯在 FINDSTR 成功时有条件地采取行动,并||
在 FINDSTR 失败时有条件地采取行动。
My :TEST
routine takes the name of the variable as the sole argument.
我的:TEST
例程将变量的名称作为唯一的参数。
Note that in some situations, the batch parser requires that you also escape the double quote as ^"
, meaning a fully escaped quote will look like \^"
. But that complication is not needed in your case. The FINDSTR escape rules are actually a bit wonky. See What are the undocumented features and limitations of the Windows FINDSTR command?for more info.
请注意,在某些情况下,批处理解析器要求您还将双引号转义为^"
,这意味着完全转义的引号看起来像\^"
。但是在您的情况下不需要这种复杂性。FINDSTR 转义规则实际上有点不稳定。请参阅Windows FINDSTR 命令有哪些未记录的功能和限制?了解更多信息。
My test code does a case sensitive search. So beta
will not match BETA
. If you want a case insensitive search, then simply add the /I
option.
我的测试代码进行区分大小写的搜索。所以beta
不会匹配BETA
。如果您想要不区分大小写的搜索,只需添加该/I
选项即可。
EDIT: 2015-07-22
编辑:2015-07-22
Another option, if and only if you want to ignore case, is to use a combination of variable expansion search/replace and expansion substring operations. Both test10 and test11 pass below.
当且仅当您想忽略大小写时,另一种选择是使用变量扩展搜索/替换和扩展子字符串操作的组合。test10 和 test11 都通过了下面的测试。
@echo off
setlocal
set test1=abc
set test2="abc"
set test3="")
set test4=")"
set test5=abc")"
set test6=xyzBETAzyx
set test7="xyz BETA zyx"
set test8=xyzBETAzyx")"
set test9=xyzBETAzyx")"
set test10="xyzbetazyx")
set test11="xyzBETAzyx")
for /l %%N in (1 1 11) do call :test test%%N
exit /b
:test variableName
setlocal enableDelayedExpansion
set "result=FAIL"
if "!%1:beta=!" neq "!%1!" if "!%1:~0,1!" equ ^""" if "!%1:~-2!" equ ^"")" set "result=PASS"
echo %1 = !result!: !%1!
exit /b
The first IF checks for beta
anywhere (case insensitive). The second IF checks for "
at the beginning, and the last IF checks for ")
at the end. The only tricky thing is figuring out how to escape the comparison string when it has an odd number of quotes.
第一个 IF 检查beta
任何地方(不区分大小写)。第二个 IF"
在开始时检查,最后一个 IF")
在结束时检查。唯一棘手的事情是弄清楚如何在比较字符串具有奇数个引号时对其进行转义。
回答by Aacini
@echo off
setlocal EnableDelayedExpansion
set quote="
set variable="This must be true: BETA")
echo Variable=!variable!
if "!variable:~0,1!" equ "!quote!" (
rem Start with "
if "!variable:BETA=!" neq "!variable!" (
rem Contains BETA
if "!variable:~-2!" equ "!quote!^)" (
echo TRUE: it start with " contains BETA and ends with "^)
) else (
echo FALSE, it does not ends with "^)
)
) else (
echo FALSE, it does not contain BETA
)
) else (
echo FALSE, it does not start with "
)
Output:
输出:
Variable="This must be true: BETA")
TRUE: it start with " contains BETA and ends with ")
The way to deal with unbalanced quotes (and with other special Batch characters, indeed) is to use Delayed Expansion: this way the quotes and special characters don't appear in the command line being parsed.
Remember to escape with ^ any right parentheses that are not closing parentheses of command blocks.
An easy way to check if a variable contain a given string is try to eliminate the string from variable value: if the result is different to the original value, then the variable contain the string.
处理不平衡引号(以及其他特殊的批处理字符)的方法是使用延迟扩展:这样引号和特殊字符就不会出现在被解析的命令行中。
记住用 ^ 转义任何不是命令块右括号的右括号。
检查变量是否包含给定字符串的一种简单方法是尝试从变量值中消除该字符串:如果结果与原始值不同,则该变量包含该字符串。
Antonio
安东尼奥