windows - 设置延迟扩展时如何检查变量的特殊字符

标签 windows batch-file

我想知道字符串是否有任何特殊字符。 我使用环境变量尝试了以下操作,这里它检测到工作正常的 @ 字符

@echo off
set x=Stack@123Overflow
echo %x%
echo %x% | findstr /r "^[^\\/?%%*:|<>\_\.-~!@#$&()+="]*$^" > nul
echo %errorlevel%
pause

输出为1:
output 1

但是,当我启用延迟扩展时,我得到的 errorlevel0,实际上应该是 1

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set x=Stack@123Overflow
echo !x!
echo !x! | findstr /r "^[^\\/?%%*:|<>\_\.-~!@#$&()+="]*$^" > nul
echo %errorlevel%
pause

这是第二个代码的输出(输出为0):
output 0

我只想使用延迟扩展来实现这一点,我想将它与 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&ampersand"           & 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&ampersand
[with&ampersand]
errorlevel = 1
----------------------------------------------------------------------
test_data=with*?wildcards
[with*?wildcards]
errorlevel = 1
----------------------------------------------------------------------
test_data=.\-/?%*:|<>_~^!@#$&()+=
[.\-/?%*:|<>_~^!@#$&()+=]
errorlevel = 1
----------------------------------------------------------------------

S:\>

关于windows - 设置延迟扩展时如何检查变量的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45504784/

相关文章:

c - 用户未与 Richtextbox 交互

c - 链接器错误 undefined symbol _log10f

C++系统函数

javascript - Windows 10 中不调用 ActiveXObject

java - Tizen 包管理器无法识别 JDK

c++ - 扫描进程并在找到特定进程时执行某些操作

windows - 为什么 Windows 将新创建文件的 "created time"属性设置为旧时间?

java - 按任意键继续不工作?

batch-file - 检查 Windows 批处理中的列表变量是否为空

batch-file - 使用命令提示符创建批处理脚本文件