Let's say I have some text in a variable called $1. Now I want to check if that $1 contains a certain string. If it contains a certain string I want to print a message. The printing is not the problem, the problem is the check...ideas how to do that?
假设我有一个变量名为$1的文本。现在我要检查$1是否包含一个字符串。如果它包含一个特定的字符串,我想要打印一条消息。印刷不是问题,问题是支票……如何做到这一点?
3 个解决方案
#1
19
The easiest way in my opinion is this :
在我看来,最简单的方法是:
set YourString=This is a test
If NOT "%YourString%"=="%YourString:test=%" (
echo Yes
) else (
echo No
)
Basiclly the string after ':' is the string you are looking for and you are using not infront of the if because %string:*% will remove the * from the string making them not equal.
Basiclly the string after ':'是你要查找的字符串,如果因为%string:*%会将*从字符串中删除,使它们不相等。
#2
9
The SET search and replace trick works in many cases, but it does not support case sensitive or regular expression searches.
在许多情况下,SET search和replace技巧都起作用,但它不支持区分大小写或正则表达式搜索。
If you need a case sensitive search or limited regular expression support, you can use FINDSTR.
如果您需要一个区分大小写的搜索或有限的正则表达式支持,您可以使用FINDSTR。
To avoid complications of escaping special characters, it is best if the search string is in a variable and both search and target are accessed via delayed expansion.
为了避免特殊字符转义的复杂性,最好将搜索字符串放在一个变量中,并且通过延迟扩展来访问搜索和目标。
You can pipe $1 into the FINDSTR command with the ECHO command. Use ECHO(
in case $1 is undefined, and be careful not to add extra spaces. ECHO !$1!
will echo ECHO is off.
(or on) if $1 is undefined, whereas ECHO(!$1!
will echo a blank line if undefined.
您可以使用ECHO命令将$1插入FINDSTR命令。使用ECHO(如果$1未定义,请注意不要添加额外空间。回声,1美元!如果$1未定义,则echo将关闭。(或on) $1未定义,而echo (!$1!如果未定义,将返回空行。
FINDSTR will echo $1 if it finds the search string - you don't want that so you redirect output to nul. FINDSTR sets ERRORLEVEL to 0 if the search string is found, and 1 if it is not found. That is what is used to check if the string was found. The &&
and ||
is a convenient syntax to use to test for match (ERRORLEVEL 0) or no match (ERRORLEVEL not 0)
如果找到搜索字符串,FINDSTR将返回$1——您不希望这样,所以您将输出重定向到nul。如果找到搜索字符串,FINDSTR将ERRORLEVEL设置为0,如果没有找到,则将其设置为1。这是用来检查字符串是否被找到的。||是一种方便的语法,用于测试匹配(ERRORLEVEL 0)或不匹配(ERRORLEVEL不是0)
The regular expression support is rudimentary, but still useful.
正则表达式支持是基本的,但仍然有用。
See FINDSTR /? for more info.
看到中/ ?更多信息。
This regular expression example will search $1 for "BEGIN" at start of string, "MID" anywhere in middle, and "END" at end. The search is case sensitive by default.
这个正则表达式示例将搜索$1在字符串开头的“开始”,中间的“MID”,以及“END”。在默认情况下,搜索是区分大小写的。
set "search=^BEGIN.*MID.*END$"
setlocal enableDelayedExpansion
echo(!$1!|findstr /r /c:"!search!" >nul && (
echo FOUND
rem any commands can go here
) || (
echo NOT FOUND
rem any commands can go here
)
#3
2
As far as I know cmd.exe has no built-in function which answers your question directly. But it does support replace operation. So the trick is: in your $1 replace the substring you need to test the presence of with an empty string, then check if $1 has changed. If it has then it did contain the substring (otherwise the replace operation would have had nothing to replace in the first place!). See the code below:
据我所知cmd。exe没有内置函数,可以直接回答您的问题。但它确实支持替换操作。因此,诀窍是:在$1替换子字符串时,您需要使用空字符串来测试是否存在,然后检查$1是否已经更改。如果它已经包含了子字符串(否则替换操作在第一个地方将没有任何替换!)看下面的代码:
set longString=the variable contating (or not containing) some text
@rem replace xxxxxx with the string you are looking for
set tempStr=%longString:xxxxxx=%
if "%longString%"=="%tempStr%" goto notFound
echo Substring found!
goto end
:notFound
echo Substring not found
:end
#1
19
The easiest way in my opinion is this :
在我看来,最简单的方法是:
set YourString=This is a test
If NOT "%YourString%"=="%YourString:test=%" (
echo Yes
) else (
echo No
)
Basiclly the string after ':' is the string you are looking for and you are using not infront of the if because %string:*% will remove the * from the string making them not equal.
Basiclly the string after ':'是你要查找的字符串,如果因为%string:*%会将*从字符串中删除,使它们不相等。
#2
9
The SET search and replace trick works in many cases, but it does not support case sensitive or regular expression searches.
在许多情况下,SET search和replace技巧都起作用,但它不支持区分大小写或正则表达式搜索。
If you need a case sensitive search or limited regular expression support, you can use FINDSTR.
如果您需要一个区分大小写的搜索或有限的正则表达式支持,您可以使用FINDSTR。
To avoid complications of escaping special characters, it is best if the search string is in a variable and both search and target are accessed via delayed expansion.
为了避免特殊字符转义的复杂性,最好将搜索字符串放在一个变量中,并且通过延迟扩展来访问搜索和目标。
You can pipe $1 into the FINDSTR command with the ECHO command. Use ECHO(
in case $1 is undefined, and be careful not to add extra spaces. ECHO !$1!
will echo ECHO is off.
(or on) if $1 is undefined, whereas ECHO(!$1!
will echo a blank line if undefined.
您可以使用ECHO命令将$1插入FINDSTR命令。使用ECHO(如果$1未定义,请注意不要添加额外空间。回声,1美元!如果$1未定义,则echo将关闭。(或on) $1未定义,而echo (!$1!如果未定义,将返回空行。
FINDSTR will echo $1 if it finds the search string - you don't want that so you redirect output to nul. FINDSTR sets ERRORLEVEL to 0 if the search string is found, and 1 if it is not found. That is what is used to check if the string was found. The &&
and ||
is a convenient syntax to use to test for match (ERRORLEVEL 0) or no match (ERRORLEVEL not 0)
如果找到搜索字符串,FINDSTR将返回$1——您不希望这样,所以您将输出重定向到nul。如果找到搜索字符串,FINDSTR将ERRORLEVEL设置为0,如果没有找到,则将其设置为1。这是用来检查字符串是否被找到的。||是一种方便的语法,用于测试匹配(ERRORLEVEL 0)或不匹配(ERRORLEVEL不是0)
The regular expression support is rudimentary, but still useful.
正则表达式支持是基本的,但仍然有用。
See FINDSTR /? for more info.
看到中/ ?更多信息。
This regular expression example will search $1 for "BEGIN" at start of string, "MID" anywhere in middle, and "END" at end. The search is case sensitive by default.
这个正则表达式示例将搜索$1在字符串开头的“开始”,中间的“MID”,以及“END”。在默认情况下,搜索是区分大小写的。
set "search=^BEGIN.*MID.*END$"
setlocal enableDelayedExpansion
echo(!$1!|findstr /r /c:"!search!" >nul && (
echo FOUND
rem any commands can go here
) || (
echo NOT FOUND
rem any commands can go here
)
#3
2
As far as I know cmd.exe has no built-in function which answers your question directly. But it does support replace operation. So the trick is: in your $1 replace the substring you need to test the presence of with an empty string, then check if $1 has changed. If it has then it did contain the substring (otherwise the replace operation would have had nothing to replace in the first place!). See the code below:
据我所知cmd。exe没有内置函数,可以直接回答您的问题。但它确实支持替换操作。因此,诀窍是:在$1替换子字符串时,您需要使用空字符串来测试是否存在,然后检查$1是否已经更改。如果它已经包含了子字符串(否则替换操作在第一个地方将没有任何替换!)看下面的代码:
set longString=the variable contating (or not containing) some text
@rem replace xxxxxx with the string you are looking for
set tempStr=%longString:xxxxxx=%
if "%longString%"=="%tempStr%" goto notFound
echo Substring found!
goto end
:notFound
echo Substring not found
:end