首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FINDSTR与FINDSTR混合时荒谬的插入转义序列

FINDSTR与FINDSTR混合时荒谬的插入转义序列
EN

Stack Overflow用户
提问于 2017-01-06 03:52:12
回答 1查看 246关注 0票数 2

我已经得到了一个字符串验证批,它使用FINDSTR处理基本的正则表达式,并且在今天我认为在十几个 ^上添加了一个之前,我无法正确地转义插入字符,所以几乎放弃了它。

失败验证命令:stringVerifier.bat "Derpy QFail" "^^^^^^^^^^^^^^^^QFail" /R

通过验证命令:stringVerifier.bat "QFail Derpy" "^^^^^^^^^^^^^^^^QFail" /R

stringVerifier.bat

代码语言:javascript
复制
@echo off

REM ===== Verify Sub-String In String Script =====
REM It uses Window's native findstr.exe commandline application with simplified options and scope for checking strings instead of file strings.
REM Call this script by preceeding the commandline call with the word `call` instead of directly running it.
REM 
REM Parameter 1 is the string to search through. This must be wrapped in double-quotes.
REM Parameter 2 is the search pattern, e.g., "^QWARN". This must be wrapped in double-quotes.
REM Parameter 3 should be either /R for Regular Expression search or /L for a string-literal search.
REM Parameter 4 is optional. It should be true/false or t/f for case-sensitive searches.
REM Parameter 4 behavior will default to false for case-sensitivity if left out of the commandline parameters when called.
REM 
REM You can check success by exit code or if the value of %match_was_found% is true/false or if %match_found% isn't empty.
REM A false value for %match_was_found% means there's no result to check due to no match occurring.
REM A non-empty value for %match_found% always indicates success, and vice-versa.
REM These values reset every time you run this script.


REM Extract between 1 from the front and 1 from the end to strip commandline quotes
Set string_to_search=%1
Set string_to_search=%string_to_search:~1,-1%
Set search_pattern=%2
Set search_pattern=%search_pattern:~1,-1%


Set search_type=%3
Set case_sensitive=%4
IF /i "%case_sensitive%"=="t" Set case_sensitive=true
IF /i "%case_sensitive%"=="f" Set case_sensitive=false
IF /i "%case_sensitive%"=="" Set case_sensitive=false


IF "%string_to_search%"=="" echo You forgot to provide parameter one, %string_to_search%, to specify your string to search, e.g., "Start of line of this string"&&exit /b 1
IF "%search_pattern%"=="" echo You forgot to provide parameter two, %search_pattern%, to specify your search pattern, e.g., "^Start of.*string$"&&exit /b 1
IF "%search_type%"=="" echo You forgot to provide parameter three, %search_type%, to specify your search type, e.g., /R or /L&&exit /b 1
IF /i NOT "%search_type%"=="/R" IF NOT "%search_type%"=="/L" echo You didn't provide the correct third parameter, %search_type%, for /R or /L&&exit /b 1
IF /i NOT "%case_sensitive%"=="" IF NOT "%case_sensitive%"=="true" IF NOT "%case_sensitive%"=="false" echo You didn't provide the correct fourth, %case_sensitive%, parameter for true or false&&exit /b 1


Set match_was_found=
Set match_found=
Set Command_To_Run=
Set Command_Ender=


Set Command_To_Run=echo.%string_to_search%


IF NOT "%case_sensitive%"=="" IF NOT "%case_sensitive%"=="true" Set Command_Ender=/I
IF "%search_type%"=="/R" (Set Command_Ender=%Command_Ender% /R %search_pattern%) ELSE (Set Command_Ender=%Command_Ender% /C:%search_pattern%)


FOR /F "tokens=*" %%i IN (' %Command_To_Run% ^| findstr.exe %Command_Ender% ') DO Set match_found=%%i


REM Deleting all variables we don't want retained as temporary env vars.
IF "%match_found%"=="" Set match_was_found=false&&Set string_to_search=&&Set search_pattern=&&Set search_type=&&Set Command_To_Run=&&Set Command_Ender=&&Set case_sensitive=&&exit /b 1
IF NOT "%match_found%"=="" Set match_was_found=true&&Set string_to_search=&&Set search_pattern=&&Set search_type=&&Set Command_To_Run=&&Set Command_Ender=&&Set case_sensitive=


REM Comment out this line or add more script logic if you want to disable console output of the matching line
echo %match_found%
exit /b 0

有没有办法避免批处理本身中这种荒谬的转义序列,而不需要生成临时文件和其他逃避regex元字符的烦扰?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-06 09:00:05

您使用的set variable语法是不利的。

由于没有引号,在任何SET命令中都将使用插入符号来转义下一个字符。

这一行将占用您的插入符号的一半。

代码语言:javascript
复制
Set search_pattern=%search_pattern:~1,-1%

您应该使用扩展集语法:

代码语言:javascript
复制
set "variable=content"

但是,您只需要更改一些行,就可以将插入符号的总数减少到两个。

代码语言:javascript
复制
Set "search_pattern=%~2"  This takes the argument and removes also the quotes
...
IF "%search_type%"=="/R" (Set "Command_Ender=%Command_Ender% /R %search_pattern%") ELSE (Set "Command_Ender=%Command_Ender% /C:%search_pattern%")
...
FOR /F "tokens=*" %%i IN (' %Command_To_Run% ^| findstr.exe %%Command_Ender%% ') DO Set match_found=%%i

现在你只需要用

代码语言:javascript
复制
stringVerifier.bat "QFail Derpy" "^^QFail" /R

这是因为最后一个findstr仍然消耗了插入符号的一次。

这也可以用引号来更改,但是您必须更改Command_Ender变量,以便只保存选项,而不再保存搜索字符串。

为了检查代码,您应该在某些时候使用set Command_Enderset search_pattern来显示变量的真实内容。

您还应该看看延迟膨胀,因为延迟扩展不会改变变量内容。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41498665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档