Skip navigation

JSI Tip 9451. How can I return the numeric characters in an input string?


I have scripted Numbers.bat to return the numeric characters in a given string.

The syntax for using Numbers.bat is:

call Numbers Input Output

Where Input contains the the input string and Output is a call directed environment variable that will contain the numeric character from Input. If no numeric characters exist in Input, Output will be NOT Defined.

Numbers.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: Numbers String StrNumbers&goto :EOF
if exist "%TEMP%\Numbers.vbs" goto doit
@echo dim str>"%TEMP%\Numbers.vbs"
@echo Set objshell = CreateObject("WScript.Shell")>>"%TEMP%\Numbers.vbs"
@echo Set objArgs = WScript.Arguments>>"%TEMP%\Numbers.vbs"
@echo str=objArgs(0)>>"%TEMP%\Numbers.vbs"
@echo wscript.echo GetNumbersOnly(str)>>"%TEMP%\Numbers.vbs"
@echo. >>"%TEMP%\Numbers.vbs"
@echo function  GetNumbersOnly(str)>>"%TEMP%\Numbers.vbs"
@echo.     with new regexp>>"%TEMP%\Numbers.vbs"
@echo.        .pattern = "\D">>"%TEMP%\Numbers.vbs"
@echo.        .global = true>>"%TEMP%\Numbers.vbs"
@echo.        GetNumbersOnly = .replace(str,"")>>"%TEMP%\Numbers.vbs"
@echo.    end with>>"%TEMP%\Numbers.vbs"
@echo end function>>"%TEMP%\Numbers.vbs"
:doit
set %2=
for /f "Tokens=*" %%a in ('cscript.exe //nologo "%TEMP%\Numbers.vbs" %1') do (
 set %2=%%a
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish