1

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.

briantist
  • 45,546
  • 6
  • 82
  • 127
Ricky
  • 33
  • 7
  • @npocmaka: I got this error message: FINDSTR: Cannot open 12.0 – Ricky Dec 31 '18 at 15:04
  • 1
    `FindStr` does not use standard regex, for digits it uses **`[0-9]`** and as **`.`** is a wildcard metacharacter, a literal **`.`** requires escaping with the **`\ `** character. Please open a Command Prompt window and enter **`FindStr /?`** to read the usage information. Also, please note that `FindStr` will return the entire line, not just a specific string within that line. – Compo Dec 31 '18 at 15:06
  • @Compo: I did, but I can't figure out how to implement an if statement while using FindStr. "^[0-9][0-9]\.[0-9]$" still gives me the same error message. – Ricky Dec 31 '18 at 15:12
  • 1
    @Ricky, that's of absolutely no use to us, as we cannot see the command or string your running the `FindStr` command against. Please [edit your question](https://stackoverflow.com/posts/53988684/edit), to include your code as it now stands. Please make sure before you do that, that your code follows the syntax as shown under the output from the usage information. – Compo Dec 31 '18 at 15:30
  • 4
    With your syntax above findstr expects `%newvalue%` to be file, not a string. You'll have to use echo: `Echo:%newvalue%|findstr "^[0-9][0-9]\.[0-9]$" >Nul 2>&1 &&(echo matched pattern)||(echo didn't match pattern)` –  Dec 31 '18 at 15:49
  • @LotPings: thank you, again! It worked flawlessly :P – Ricky Dec 31 '18 at 15:54

1 Answers1

1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143