我想知道字符串是否有任何特殊字符。
我使用环境变量尝试了以下操作,这里它检测到工作正常的 @
字符
@echo off
set x=Stack@123Overflow
echo %x%
echo %x% | findstr /r "^[^\\/?%%*:|<>\_\.-~!@#$&()+="]*$^" > nul
echo %errorlevel%
pause
但是,当我启用延迟扩展时,我得到的 errorlevel
为 0
,实际上应该是 1
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set x=Stack@123Overflow
echo !x!
echo !x! | findstr /r "^[^\\/?%%*:|<>\_\.-~!@#$&()+="]*$^" > nul
echo %errorlevel%
pause
我只想使用延迟扩展来实现这一点,我想将它与 wmic (wmic logicaldisk get VolumeName
) 命令一起使用
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=0
FOR /F "tokens=1 delims= " %%F IN ('wmic logicaldisk get VolumeName') DO (
for /f "tokens=1 delims= " %%B in ("%%F") do (
set x=%%B
echo !x!
echo !x! | findstr /r "^[^\\/?%%*:|<>\_\.-~!@#$&()+="]*$^" > nul
echo %errorlevel%
)
)
pause
我也用 !errorlevel!
代替 %errorlevel%
试过了,但没用。
最佳答案
解析器在准备要在管道内执行的命令时处理转义字符时出现问题。传递给 findstr
的正则表达式变为
[\\/?%*:|<>\_\.-~@#$&()+=]*$
试试
echo !x! | findstr /r "^^[^^\\/?%%*:|<>\_\.-~^!@#$&()+=""]*$"
并且,正如 jeb 所指出的,在您的上一个代码中,您必须将 echo %errorlevel%
(解析 for
命令时变量被扩展)更改为 echo !errorlevel!
(变量在执行 echo
命令之前展开)
编辑以适应评论
@echo off
setlocal enableextensions disabledelayedexpansion
set "test_data=clean" & call :runTest
set "test_data=with spaces" & call :runTest
set "test_data=with ending spaces " & call :runTest
set "test_data= with starting spaces" & call :runTest
set "test_data=with!exclamation" & call :runTest
set "test_data=with%%percentsign" & call :runTest
set "test_data=with^caret" & call :runTest
set "test_data=with:colon" & call :runTest
set "test_data=with""quotes" & call :runTest
set "test_data=with&ersand" & call :runTest
set "test_data=with*?wildcards" & call :runTest
set "test_data=.\-/?%%*:|<>_~^!@#$&()+=" & call :runTest
goto :eof
:runTest
setlocal enabledelayedexpansion
set test_data
echo [!test_data!]
(cmd /v/c"(@echo !test_data!)")| >nul findstr /x /r /c:"[^^ .\-\\/?%%*:|<>_~^!@#$&()+=""^^]*"
echo errorlevel = !errorlevel!
endlocal
echo ----------------------------------------------------------------------
输出:
S:\>q45504784.cmd
test_data=clean
[clean]
errorlevel = 0
----------------------------------------------------------------------
test_data=with spaces
[with spaces]
errorlevel = 1
----------------------------------------------------------------------
test_data=with ending spaces
[with ending spaces ]
errorlevel = 1
----------------------------------------------------------------------
test_data= with starting spaces
[ with starting spaces]
errorlevel = 1
----------------------------------------------------------------------
test_data=with!exclamation
[with!exclamation]
errorlevel = 1
----------------------------------------------------------------------
test_data=with%percentsign
[with%percentsign]
errorlevel = 1
----------------------------------------------------------------------
test_data=with^caret
[with^caret]
errorlevel = 1
----------------------------------------------------------------------
test_data=with:colon
[with:colon]
errorlevel = 1
----------------------------------------------------------------------
test_data=with""quotes
[with""quotes]
errorlevel = 1
----------------------------------------------------------------------
test_data=with&ersand
[with&ersand]
errorlevel = 1
----------------------------------------------------------------------
test_data=with*?wildcards
[with*?wildcards]
errorlevel = 1
----------------------------------------------------------------------
test_data=.\-/?%*:|<>_~^!@#$&()+=
[.\-/?%*:|<>_~^!@#$&()+=]
errorlevel = 1
----------------------------------------------------------------------
S:\>
关于windows - 设置延迟扩展时如何检查变量的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45504784/