I am trying to extract number alone from string and using below regular expression looks like it is not helping out .
set d=22222112k
set CR =echo %d%|FindStr /R "^[0123-9]*$"
echo %CR%
how can i extract number alone from variable ?
I am trying to extract number alone from string and using below regular expression looks like it is not helping out .
set d=22222112k
set CR =echo %d%|FindStr /R "^[0123-9]*$"
echo %CR%
how can i extract number alone from variable ?
You can use Regex in vbscript with a batch file to extract number like this code : Extract_Number.bat
@echo off & color 0A
Title Extract Number from String using Regex with vbscript
Set "InputSring=22222112k"
Call :Extract_Number "%InputSring%" Number
Echo The Number extracted is %Number%
Pause & Exit
::-----------------------------------------------------------------------------------
:Extract_Number <InputString> <Variable to be Set>
(
echo WScript.StdOut.WriteLine Extract_Number(Data^)
echo Function Extract_Number(Data^)
echo Data = WScript.Arguments(0^)
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\d+"
echo For Each Match in re.Execute(Data^)
echo Number = Match.Value
echo Next
echo Extract_Number = Number
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript //nologo "%tmp%\%~n0.vbs" "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------
EDIT : Or you can do it with Powershell too like this code below :
@echo off & color 0A
Title Extract Number from String using Regex with Powershell
Set "InputSring=22222112k"
Set psCmd="&{$re=[regex]'\d+'; $re.Match('%InputSring%').value}"
Call :RunPS %psCmd% Number
Echo The Number extracted is %Number%
pause & Exit
::--------------------------------------------------------------------
:RunPS <PassPSCMD> <RetValue>
for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::--------------------------------------------------------------------