我如何批量进行正则表达式否定匹配? [英] How can I do a negative regex match in batch?

查看:261
本文介绍了我如何批量进行正则表达式否定匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这次,我无法为之前的

如果不是"^\d\d\.\d$",则将newvalue设置为空白.仅当找不到newvalue时将其留空,因为它会产生随机结果.

有人可以帮我吗?进行此if语句的最佳方法是什么?

谢谢.

解决方案

LotPings 发布的非常有效的解决方案是:

Echo:%newvalue%|findstr "^[0-9][0-9]\.[0-9]$" >Nul 2>&1 &&(echo matched pattern)||(echo didn't match pattern)

我建议使用一些单线解决方案:

echo:%newvalue%|%SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul && (echo matched pattern) || (echo didn't match pattern)

或者更容易阅读和处理分配给环境变量newvalue的任何字符串值:

setlocal EnableExtensions EnableDelayedExpansion
echo:!newvalue!| %SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul
if errorlevel 1 (
    echo Value does not match the regular expression pattern.
) else (
    echo Value matches the regular expression pattern.
)
endlocal

在命令echo和分配给环境变量newvalue的字符串之间使用冒号代替空格,以避免在 ECHO 输出命令回显的当前状态>完全没有定义.

最后一个解决方案使用延迟扩展,以避免带有 ECHO的命令行 FINDSTR 不起作用,或者如果分配给变量newvalue的字符串包含像&<>|这样的运算符,则与写的完全不同.

重要的是,重定向操作符|上应留有空间,该操作符会将命令 ECHO 的输出通过管道传递给命令 FINDSTR 作为输入,因为这样会引起空格也可以通过命令 ECHO 输出,并且正则表达式find永远不会为正.如上例所示,|的空格权无关紧要.

FINDSTR 不支持\d表示任何数字.必须在方括号中的自定义字符类中指定要匹配的字符. FINDSTR [0-9]字符0123456789¹²³匹配.必须使用[0123456789]来仅匹配字符0123456789而不匹配¹²³.

.表示任何字符,除了点用反斜杠转义以外,在这种情况下将其解释为文字字符.

"..."中的搜索字符串默认情况下由 FINDSTR 解释为正则表达式字符串.但是我认为,最好使用/L/R使其对于 FINDSTR 以及每个阅读器100%清晰的注释,如何将搜索字符串解释为原义或原义.正则表达式字符串.

FINDSTR 退出时,在搜索到的输入字符流上不存在正匹配时,值为1,在至少一个正匹配中为0.可以如上所示评估 FINDSTR 的退出代码,并使用Windows在单行中包含多个命令的情况下进行详细说明批处理文件.

在正匹配项上的 FINDSTR 的输出没有意义,因此重定向到设备 NUL 对其进行抑制.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • echo /?
  • endlocal /?
  • findstr /?
  • if /?
  • setlocal /?

另请参阅有关回声.未能提供文本或空白行-而是使用ECHO/,原因是为什么在echo之后使用:通常比从文件读取或用户输入的字符串的输出空间更好./p>

This time, I am unable to create an if statement for my previous post

I'm trying to check if the value is in this form: ^\d\d\.\d$

Most of the time, it will. However, it is sometimes unavailable.

In other scripting languages, I can manage it, but I cannot, for some unknown reasons, figure out how to do it in batch.

So, it should be something like:

if not findstr /R "^\d\d\.\d$" %newvalue%
set newvalue=

If it's not "^\d\d\.\d$", then set newvalue blank. This is only to blank newvalue when it is not found because it will give random results.

Can somebody help me out? What's the best way to do this if statement?

Thanks in advance.

解决方案

The very good working solution posted by LotPings is:

Echo:%newvalue%|findstr "^[0-9][0-9]\.[0-9]$" >Nul 2>&1 &&(echo matched pattern)||(echo didn't match pattern)

I suggest a little bit different single line solution:

echo:%newvalue%|%SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul && (echo matched pattern) || (echo didn't match pattern)

Or easier readable and working for really any string value assigned to environment variable newvalue:

setlocal EnableExtensions EnableDelayedExpansion
echo:!newvalue!| %SystemRoot%\System32\findstr.exe /R "^[0123456789][0123456789]\.[0123456789]$" >nul
if errorlevel 1 (
    echo Value does not match the regular expression pattern.
) else (
    echo Value matches the regular expression pattern.
)
endlocal

A colon is used between command echo and the string assigned to environment variable newvalue instead of a space to avoid that command ECHO outputs the current status of command echoing in case of newvalue is not defined at all.

The last solution uses delayed expansion to avoid that the command line with ECHO and FINDSTR does not work or does something completely different than it is written for if the string assigned to variable newvalue contains operators like &<>|.

It is important that there is no space left to redirection operator | which pipes output of command ECHO to the command FINDSTR as input because of this space character would be also output by command ECHO and the regular expression find would never be positive. A space right to | does not matter as last example demonstrates.

FINDSTR does not support \d as representation for any digit. It is necessary to specify the characters to match in a self-defined character class in square brackets. FINDSTR matches with [0-9] the characters 0123456789¹²³. It is necessary to use [0123456789] to match just the characters 0123456789 without ¹²³.

. means any character, except the dot is escaped with a backslash in which case it is interpreted as literal character.

A search string in "..." is interpreted by FINDSTR by default as regular expression string. But I think, it is always good to make use of /L or /R to make it 100% clear for FINDSTR and for every reader how the search string should be interpreted, as literal or as regular expression string.

FINDSTR exits with value 1 on no positive match on searched input character stream and 0 on at least one positive match. The exit code of FINDSTR can be evaluated as shown above and described in detail in single line with multiple commands using Windows batch file.

The output of FINDSTR on a positive match is of no interest and therefore redirected to device NUL to suppress it.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • findstr /?
  • if /?
  • setlocal /?

See also the Microsoft article about Using command redirection operators and DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the reason why using : after echo is often better than a space on output of a string read from a file or entered by a user.

这篇关于我如何批量进行正则表达式否定匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆