2

How can I check contains of substring in string variable of batch file?

for example:

set var1=foobarfoo

set regexp=.*bar.*

if [check var1 by regexp] echo "YES"

In my case, it's must be only check by regular expression and only in .bat file.

disable1992
  • 4,873
  • 3
  • 17
  • 25

1 Answers1

5

Adjusting the answer found here give

@echo off
set var1=foobarfoo
set "regexp=.*bar.*"
echo %var1%
echo %regexp%
setlocal enableDelayedExpansion
echo(%var1%|findstr /r /c:"%regexp%" >nul && (
    echo FOUND
    rem any commands can go here
) || (
    echo NOT FOUND
    rem any commands can go here
)
Community
  • 1
  • 1
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56