Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - Compare variable with regular expression

I'm doing a batch script that has to check if there are some programs installed on the computer. For that, I execute programName --version and I store the output in a variable. The problem is when I try to compare with a regular expression (only to know if this program exists in the machine). I'm trying this code, but does't work

>output.tmp node --version
<output.tmp (set /p hasNode= )
if "%hasNode%" == "[vV][0-9.]*" (echo Has node) else (echo You have to install node)

If I change the regular expression for the output of this command works properly, so I suppose that I'm doing a bad use of the regular expression (I've checked it and it's fine for the command's output)

Thanks four your help guys

like image 369
jgarciabt Avatar asked Apr 19 '14 11:04

jgarciabt


1 Answers

Batch/cmd does not support regexes directly. You have to use findstrfor that, for example:

echo %node% | findstr /r "[vV][0-9.]*" >nul 2>&1 && (echo contains) || (echo does not contain) or

echo %node% | findstr /r "[vV][0-9.]*" >nul 2>&1 if errorlevel 1 (echo does not contain) else (echo contains)

This trick delegates comparison to findstr and than uses return code (errorlevel) from it.
(please note that regexes findstr supports are also quite limited and has some quirks, more info http://ss64.com/nt/findstr.html)

like image 110
wmz Avatar answered Sep 22 '22 10:09

wmz