我想批量匹配一个变量与另一个变量的部分内容。这是我想做的一些伪代码。
set h= Hello-World
set f= This is a Hello-World test
if %h% matches any string of text in %f% goto done
:done
echo it matched
有谁知道我怎么能做到这一点?
我想批量匹配一个变量与另一个变量的部分内容。这是我想做的一些伪代码。
set h= Hello-World
set f= This is a Hello-World test
if %h% matches any string of text in %f% goto done
:done
echo it matched
有谁知道我怎么能做到这一点?
根据此处的答案,您可以使用FINDSTR
命令来使用/C
开关比较字符串(从链接的答案修改,因此您不必有单独的批处理文件来比较字符串):
@ECHO OFF
set h=Hello-World
set f=This is a Hello-World test
ECHO Looking for %h% ...
ECHO ... in %f%
ECHO.
echo.%f% | findstr /C:"%h%" 1>nul
if errorlevel 1 (
ECHO String "%h%" NOT found in string "%f%"!
) ELSE (
ECHO String "%h%" found in string "%f%"!
)
ECHO.
PAUSE
如果满足以下条件:
=
!
那么你可以使用:
@echo off
setlocal enableDelayedExpansion
set h=Hello-World
set f=This is a Hello-World test
if "!f:*%h%=!" neq "!f!" (
echo it matched
) else (
echo it did not match
)
搜索词*
前面只需要允许搜索词以 开头*
。
可能还有其他一些涉及引号和特殊字符的情况,上述情况可能会失败。我相信以下应该解决这些问题,但原来的限制仍然适用:
@echo off
setlocal enableDelayedExpansion
set h=Hello-World
set f=This is a Hello-World test
for /f delims^=^ eol^= %%S in ("!h!") do if "!f:*%%S=!" neq "!f!" (
echo it matched
) else (
echo it did not match
)
这是另一种方式:
@echo off
set "h=Hello-World"
set "f=This is a Hello-World test"
call set "a=%%f:%h%=%%"
if not "%a%"=="%f%" goto :done
pause
exit /b
:done
echo it matched
pause