Page 1 of 1

Extract numbers only from a string - optimization

Posted: 05 Jul 2012 00:10
by miskox
Hi all,

I have this simple code (it works, but it is slow):

Code: Select all

@echo off
set mystring=x56x723@c8
echo !%mystring%!
call :NUMBERS_ONLY "%mystring%" mystring
echo !%mystring%!

set mystring=x2c3c4c5v6
echo !%mystring%!
call :NUMBERS_ONLY "%mystring%" mystring
echo !%mystring%!

goto :EOF






:NUMBERS_ONLY
set _source=%~1
set _out=%2
set /a _cnt=0
set newstring=&rem
:00
call set tmpstring=%%_source:~%_cnt%,1%%
if "%tmpstring%"=="" set %_out%=%newstring%&&goto :EOF
if "%tmpstring%"=="0" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="1" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="2" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="3" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="4" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="5" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="6" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="7" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="8" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="9" set newstring=%newstring%%tmpstring%
set /a _cnt=_cnt + 1
goto :00


Output:

Code: Select all

!x56x723@c8!
!567238!
!x2c3c4c5v6!
!23456!


Is it possible to optimize it?

Thanks,
Saso

Re: Extract numbers only from a string - optimization

Posted: 05 Jul 2012 00:46
by foxidrive
miskox wrote:I have this simple code (it works, but it is slow):

Is it possible to optimize it?



Code: Select all

@echo off
set mystring=x56x723@c8
echo %mystring%
for /f "tokens=1-20 delims=abcdefghijklmnopqrstuvwxyz!@#$&*()-=" %%a in ("%mystring%") do echo "%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j%%k%%l%%m%%n%%o"
pause