0

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 ?

Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • 1
    `findstr` only has a very crippled REGEX support. It *always* returns the whole line. If you want easy access to "the whole REGEX package", google for `jrepl.bat` – Stephan Oct 09 '19 at 17:33
  • 2
    By the way: you don't have `%CR%` - you defined `%CR %`. Don't use spaces around the `=` – Stephan Oct 09 '19 at 17:38
  • 1
    Can you please provide an actual task instead of a generic example, there may be a simpler solution. – Compo Oct 09 '19 at 18:01
  • 1
    To assign the output of a command to a variable take a look at [How to set commands output as a variable in a batch file](https://stackoverflow.com/q/6359820) and [Assign command output to variable in batch file](https://stackoverflow.com/q/16203629)... – aschipfl Oct 09 '19 at 18:21
  • You should use powershell or vbscript for using Regex ! – Hackoo Oct 09 '19 at 18:23
  • Easy with [JREPL.BAT](https://www.dostips.com/forum/viewtopic.php?f=3&t=6044): `call jrepl "^\d+" "" /match /s d /rtn CR` – dbenham Oct 09 '19 at 21:03

1 Answers1

0

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
::--------------------------------------------------------------------
Hackoo
  • 15,943
  • 3
  • 28
  • 59