Advertisement
T3RRYT3RR0R

Batch regex restrict input

Oct 23rd, 2020 (edited)
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.78 KB | None | 0 0
  1. :GetIN [ReturnVar] ["qouted findstr search pattern"]  [Optional:"/C" - required for patterns with spaces]
  2. @Echo off
  3.  SETLOCAL EnableExtensions EnableDelayedExpansion
  4. rem - lines prefixed with ::: form part of this scripts Help Documentation. Do not remove.
  5. > "%TEMP%\%~n0_help.~tmp" (For /F "Delims=" %%G in ('Type "%~f0"^| Findstr.exe /BLIC:":::" 2^> nul ')Do For /F "Tokens=1* Delims=:" %%v in ("%%G")Do Echo/^|%%v)
  6. :::================================================================================================================
  7. :::     [ GetIn Input function or standalone script for validating input with findstr regular expressions. ]
  8. :::                     Version = 2.2        Release = October 2020       Author = T3RRY
  9. :::========================================= ^^^! important notes on usage ^^^! =========================================
  10. :::          ^^^! Pipes not supported ^^^!
  11. :::    ^     + Not suited for use with pipes due to forced loop for failed matches
  12. :::  < ^^^! >   + Test your regex pattern to ensure it is valid for the required input
  13. :::    v       format to prevent your user/s being stuck in a goto loop.
  14. :::            Error output for invalid search patterns is not displayed.
  15. :::          + EXIT.CODES - Errorlevel assessment after call:
  16. :::            - Errorlevel 3 - User has failed to provide input * OR * regex pattern is
  17. :::              invalid * OR * GetIn is a called label in a file that has recieved input
  18. :::              from a pipe that has not been handeled before the call.
  19. :::            - Errorlevel 2 - GetIn is a script that has been invoked by a pipe
  20. :::            - Errorlevel 1 - GetIn help has been Invoked * OR * GetIn has been called
  21. :::              without Arg 2 for Regex Pattern
  22. :::            - Errorlevel 0 - Input has been assigned that matches the regex Pattern
  23. :::================================================================================================================
  24. rem [* Display GetIn Usage with findstr regex info and practical examples *]
  25. >> "%TEMP%\%~n0_help.~tmp" (
  26.   Echo/^| USAGE: Call %~0 [ReturnVar] ["findstr search pattern"] [Optional:"/C" -required for patterns with spaces]
  27.   Echo/^|================================================================================================================
  28.   Echo/^| Examples [ Description is replaced with Arg 1-ReturnVar ]:
  29.   Echo/^|
  30.   Echo/^|    Call %~0 4-Digit-Number "[0-9][0-9][0-9][0-9]"
  31.   Echo/^|    Call %~0 Alphanumeric-String "[0-9a-zA-Z]*"
  32.   Echo/^|    Call %~0 semicolon-delimited-hexadecimal-String "[0-9a-fA-F][0-9a-fA-F][:][0-9a-fA-F][0-9a-fA-F]"
  33.   Echo/^|    Call %~0 AlphaString-with-3-Digit-Numeric-suffix "[a-zA-Z]*[0-9][0-9][0-9]"
  34.   Echo/^|    Call %~0 list-of-delimited-words "[a-zA-Z, ]*" /C
  35.   Echo/^|    Call %~0 pair-of-delimited-numbers "[0-9]*[, ][0-9]*" /C
  36.   Echo/^|
  37.   Echo/^|    ^^     Limit of 15 character class expressions applies
  38.   Echo/^|  ^< ^^^! ^>   Case sensitive classes should use actual letters as [STRING] instead of range [A-Z]
  39.   Echo/^|    v     See: https://stackoverflow.com/a/20159191/12343998
  40.   Echo/^|                                                     -Note: %~0 does not support Input from pipes.
  41.   Echo/^|----------------------------------------------------------------------------------------------------------------
  42.   For /F "Delims=" %%G in (' Findstr.exe /? ^| More +33 ') Do Echo/^|  %%G
  43.   Echo/^|  https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/findstr
  44.   Echo/^|  recommended reference: https://stackoverflow.com/questions/8844868/
  45.    Echo/\================================================================================================================
  46.  )
  47. (Set LF=^
  48.  
  49. %= Linefeed. do not modify. =%)
  50. Set "Exit.Code=0"
  51. rem [ test if standalone script - if so test if accessed by pipe; reject execution if true ]
  52.  If "%~0" == "%~n0" For %%G in (!cmdcmdline!)Do Echo/%%~G|Findstr.exe /LIC:"/S" > nul 2> nul && (Set "Exit.Code=2" & Goto :Usage)
  53. :retry
  54.  Del /Q "%TEMP%\%~n0_validate.~tmp" 2> nul
  55.  If !Exit.Code! GTR 5 Goto :Usage
  56.  Set "rVar=" & Set "_C=0" & Set "_S=0"
  57.  If "%~2" == "" GOTO :Usage
  58.  Setlocal DISABLEdelayedExpansion & rem ::: dispay occurances of carets [^] in pattern
  59.  Endlocal & Set "Pattern=%~2"
  60.   Set /P "rVar=Input Format for %1:!LF!!Pattern!!LF!!LF!Enter %1: "
  61.  > "%TEMP%\%~n0_validate.~tmp" (Echo/!rVar!)
  62.  If /I "%~3" == "/C" (
  63.   TYPE "%TEMP%\%!!|%~2" > nul || (Set /A "Exit.Code+=1" & <nul Set /P"= ^! Invalid value.!LF!" & Goto :Retry)
  64.  ) Else (
  65.   TYPE "%TEMP%\%~n0_validate.~tmp"| findstr.exe /RX "^%~2$" >nul || (Set /A "Exit.Code+=1" & <nul Set /P"=- ^! Invalid value.!LF!" & Goto :Retry)
  66.  )
  67.  Echo/%1: [!rVar!] Accepted
  68.  ENDLOCAL & Set "%~1=%rVar%"
  69.  Del /Q "%TEMP%\%~n0_validate.~tmp"
  70.  Exit /B 0
  71. :Usage
  72.  If "%Exit.Code%" == "0" (Set "Exit.Code=1")
  73.  If "%Exit.Code%" == "2" (
  74.   >> "%TEMP%\%~n0_help.~tmp" (
  75.     Echo/-------------------------------              ^^! pipes not supported ^^!              -------------------------------
  76.     Echo/=================================================================================================================
  77.  ))
  78. If %Exit.Code% GTR 5 (
  79.   >> "%TEMP%\%~n0_help.~tmp" (
  80.    Echo/-------------------------------      ^^! Attempt limit exceeded. Input rejected ^^!   -------------------------------
  81.    Echo/-------------------------------              ^^! pipes not supported ^^!              -------------------------------
  82.    Echo/=================================================================================================================
  83.   )
  84.   Set "Exit.Code=3"
  85.  )
  86.  Type "%TEMP%\%~n0_help.~tmp" | ( More )
  87.  Del /Q "%TEMP%\%~n0_help.~tmp" 2> nul
  88. ENDLOCAL & exit /B %Exit.Code%
  89. rem ::: [ ------------------------------------------------------------------------------------------------- End GetIn ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement