regex - regular expressions batch -



regex - regular expressions batch -

i'm writing calculator on cmd enabled binary operations. need validate input info (remove letters, , other symbols not required arithmetic operations)

class="lang-bash prettyprint-override">@echo off set data= echo %* | findstr /r "\/\? echo" > nul if "%errorlevel%" equ "0" goto printhelp :main set data= %data%%1 shift if "%1" == "" ( echo %data% | findstr /r "^[0123456789*-+()/%!^_&|]*$" >nul 2>&1 if "%errorlevel%" equ 0 ( echo wrong input info exit /b ) goto :result ) else ( goto :main ) :result set /a data="%data%" echo %data% exit /b :printhelp echo. echo -------------------------------------------------- echo using: calculator.bat [/?] [expression] echo helps consider arithmetic in command line echo -------------------------------------------------- exit /b

my regular look not working. not considered binary operation. problem?

part 1 - why "regex not working"

your logic wrong. findstr sets errorlevel 0 if there match, 1 if no match. regex verifying characters "valid", status treating match wrong input.

your if statement uses quotes on 1 side, not on other. must consistent, otherwise can never evaluate true.

percent literals must doubled within batch script. regex has percent literal should written %%.

you utilize %errorlevel% in same code block sets value. cannot work because value expanded when code block parsed - before value set.

the simplest alternative utilize if errorlevel 1, returns true if errorlevel >= 1.

another alternative enable delayed expansion setlocal enabledelayedexpansion @ top, , utilize if !errorlevel! neq 0. require quoted ! literal in regex escaped ^!, , ^ literal escaped ^^.

my favorite alternative utilize && , || conditional operators instead of if.

findstr ... >nul && (match found statements) || (no match statements)

in case, want take action if there no match, need || operator.

part 2 - why whole concept not idea

your validation overly simplistic. screening out invalid characters not prevent errors. example, 1**2 result in error, though characters "valid". there many other inputs "valid" characters result in error.

set /a can work straight environment variables. knows how access value without expanding value in code. can powerful tool. variable name used in computation can include character not operator. 1 can argue there no invalid characters set /a computations. exclusion of "invalid" characters prevents utilize of variables in computations.

below simple batch calculator programme wrote time ago. in endless loop requesting input , displaying result, until come in quit command. supports operators supported set /a.

it allows define , utilize variables in expressions. result of recent computation stored in variable named #.

the calculator can display results decimal, hexidecimal, or binary.

by default, displays result of lastly computation. can instructed display value of variables after each computation.

you can come in command instead of math computation. commands begin \

\ quit \v toggle variable listing on or off \d decimal mode - results displayed decimal \h hex mode - results displayed hexidecimal \b binary mode - results displayed binary \c x clear variable x \c * clear variables \c x* clear variables begin x

entering nil list defined variables.

cleared variables undefined. note undefined variable has implicit value of 0.

here code:

class="lang-none prettyprint-override">@echo off setlocal enabledelayedexpansion /f "delims==" %%v in ('set') set %%v= set __skip=#comspec#pathext#prompt#__mode#__str#__skip#__clr###__dispvars# set __mode=dec set __dispvars=0 :top echo: set __str= set /p "__str=%__mode%> " if "!__str!"=="\" exit /b if "!__str!"=="" phone call :dispvar # & phone call :dispvars & goto :top if /i "!__str:~0,2!"=="\c" phone call :clearvars &goto :top if /i "!__str!"=="\h" (set __mode=hex) ^ else if /i "!__str!"=="\d" (set __mode=dec) ^ else if /i "!__str!"=="\b" (set __mode=bin) ^ else if /i "!__str!"=="\v" (set /a "__dispvars=^!__dispvars") ^ else set /a #=(!__str!) phone call :dispvar # if !__dispvars! gtr 0 phone call :dispvars goto :top :clearvars /f "delims=,; " %%v in ("!__str:~2!") ( set __clr=%%v if "!__clr:~-1!"=="*" ( set __clr=!__clr:~0,-1! /f "delims==" %%x in ('set !__clr!') ( if "!__skip:#%%x#=!"=="!__skip!" set "%%x=" ) ) else set "%%v=" ) phone call :dispvar # phone call :dispvars exit /b :dispvars setlocal /f "tokens=1,2 delims==" %%v in ('set') if "!__skip:#%%v#=!"=="!__skip!" phone call :dispvar %%v exit /b :dispvar var setlocal if !__mode!==hex phone call :num2hex %1 disp if !__mode!==bin phone call :num2bin %1 disp if !__mode!==dec set /a disp=!%~1! set var=%~1 if "!var:~0,6!"=="!var!" ( set "var=!var! ----------" set "var=!var:~0,6!" ) echo %var% = !disp! exit /b :num2hex numval rtnvar setlocal enabledelayedexpansion set hex= set /a "dec=%~1" set "map=0123456789abcdef" /l %%n in (1,1,8) ( set /a "d=dec&15,dec>>=4" %%d in (!d!) set "hex=!map:~%%d,1!!hex!" ) (endlocal & rem homecoming values set %~2=%hex% exit /b ) exit /b :num2bin numval rtnvar setlocal enabledelayedexpansion set bin= set /a "dec=%~1" /l %%n in (1,1,32) ( set /a "d=dec&1,dec>>=1" set "bin=!d!!bin!" ) (endlocal & rem homecoming values set %~2=%bin% exit /b ) exit /b

and here results of short session:

class="lang-none prettyprint-override">d:\test>calculate.bat dec> 2*3 # ---- = 6 dec> a=#+1 # ---- = 7 dec> # ---- = 7 ---- = 7 dec> b=(a+=5)*2 # ---- = 24 dec> \v # ---- = 24 ---- = 12 b ---- = 24 dec> c=b/3 # ---- = 8 ---- = 12 b ---- = 24 c ---- = 8 dec> \h # ---- = 00000008 ---- = 0000000c b ---- = 00000018 c ---- = 00000008 hex> \b # ---- = 00000000000000000000000000001000 ---- = 00000000000000000000000000001100 b ---- = 00000000000000000000000000011000 c ---- = 00000000000000000000000000001000 bin> \ d:\test>

regex batch-file cmd

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -