将findstr与正则表达式一起使用时,cmd批处理文件上出现“此时异常”错误

时间:2019-03-13 08:50:34

标签: regex batch-file findstr

我有这个批处理脚本:

setlocal enableDelayedExpansion

set filename=%1%
set archive=%2%
set aleph=%3%
set bet=%4%
set gimel=%5%



if not x%filename:FEDBKA=%==x%filename% (

findstr /R <SHEM-HAKOVETZ>.*?(?:aa|tt|cc|bb|dd).*?<\/SHEM-HAKOVETZ> %1%
if !errorlevel! == 0 (
    copy %filename% %archive%
    move %filename% %gimel%
    exit
)
findstr /R <SHEM-HAKOVETZ>.*?(?:rr|yy).*?<\/SHEM-HAKOVETZ> %1%
if !errorlevel! == 0 (
    copy %filename% %archive%
    move %filename% %aleph%
    exit
)
findstr /R <SHEM-HAKOVETZ>.*?(?:ww|oo|pp).*?<\/SHEM-HAKOVETZ> %1%
if !errorlevel! == 0 (
    copy %filename% %archive%
    move %filename% %bet%
    exit
)
exit
)

运行脚本时,我得到:

。*?这是出乎意料的。

我正在寻找xml标记模式,例如:

<SHEM-HAKOVETZ>104000514813450ttS000005201811221405351025.DAT</SHEM-HAKOVETZ>

其中“ tt”可以与正则表达式中的其他选项一起切换

如果我用简单的findstr表达式替换正则表达式:

findstr "aa tt cc bb dd" %1%

一切正常,但不能满足我的确切需求。

1 个答案:

答案 0 :(得分:0)

您的findstr正则表达式无法运行because as far as I know,没有|作为OR运算符。

但是您可以这样尝试:

findstr /R /C:"<SHEM-HAKOVETZ>.*aa.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*tt.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*bb.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*cc.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*dd.*<\/SHEM-HAKOVETZ>" %FILENAME%


findstr /R /C:"<SHEM-HAKOVETZ>.*rr.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*yy.*<\/SHEM-HAKOVETZ>" %FILENAME%


findstr /R /C:"<SHEM-HAKOVETZ>.*ww.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*oo.*<\/SHEM-HAKOVETZ>" ^
    /C:"<SHEM-HAKOVETZ>.*pp.*<\/SHEM-HAKOVETZ>" %FILENAME%