Discussion:
Remove all but digits
(too old to reply)
Tom Del Rosso
2020-05-22 14:30:58 UTC
Permalink
The objective is to remove everything except digits 0-9 from a string.

%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.

set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)

But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.

If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.

I'm tempted to use GWBASIC (MS just made it open-source!).


--
Zaidy036
2020-05-22 16:23:38 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
ASAP Utilities has this function (free for non-business use)
Herbert Kleebauer
2020-05-22 18:28:52 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If there is only a problem with "=" you can use:

@echo off
set a=3x3=4+5 = 1 + 8
echo %a%

set b=
for %%i in (%a%) do call set b=%%b%%%%i
echo %b%

set d=%b%

set c=
:loop
for /f "tokens=1* delims=0123456789" %%i in ("%b%") do set c=%c% %%i&set b=%%j
if not [%b%]==[] goto :loop
echo %c%

for %%i in (%c%) do call set d=%%d:%%i=%%

echo %d%
Tom Del Rosso
2020-05-24 22:49:19 UTC
Permalink
Post by Herbert Kleebauer
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a
string. %alpha_substrings% does contain all possible non-numeric
substrings
(space separated), so this should remove them and leave only the
digits. set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
@echo off
set a=3x3=4+5 = 1 + 8
echo %a%
set b=
for %%i in (%a%) do call set b=%%b%%%%i
echo %b%
set d=%b%
set c=
Post by Tom Del Rosso
loop
for /f "tokens=1* delims=0123456789" %%i in ("%b%") do set c=%c%
%%i&set b=%%j if not [%b%]==[] goto :loop
echo %c%
for %%i in (%c%) do call set d=%%d:%%i=%%
echo %d%
Thank you (all 3). I incorporated this method and it worked, but
yesterday realized that I also had to extract some information about the
place in the string where the longest number is. So I had to scrap the
whole routine and use brute force scanning and parsing of each
character, which is one of the methods in Zaidy's link. At least
special characters aren't a problem any more.

Thank you again.
p***@berkeley.edu
2020-05-22 19:51:39 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
--
Herbert has provided an elegant solution, as always. Here's another approach:

File=OnlyNumbers.cmd

Line ····.····1····.····2····.····3····.····4····.
00001. @echo off
00002. setlocal
00003. set inpt=%*
00004. :char
00005. set char=%inpt:~0,1%
00006. set inpt=%inpt:~1%
00007. echo/%char%|FindStr /R "[0-9]" > nul:
00008. if %errorlevel% equ 0 set out=!out!%char%
00009. if defined inpt goto :char
00010. echo/%*
00011. echo/%out%
00012. endlocal& set %~n0=%out%&goto :EOF


Here's some sample output:

C:\Users\JoeUser\temp: onlynumbers 3x3= 1 + 8 = 7 + 2
3x3= 1 + 8 = 7 + 2
331872

C:\Users\JoeUser\temp: onlynumbers abc 123 a29832hadffgwe3r384rheuw z43828
abc 123 a29832hadffgwe3r384rheuw z43828
12329832338443828

(The numbers are returned in variable OnlyNumbers)
Zaidy036
2020-05-24 21:19:12 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
Look at <https://www.dostips.com/forum/viewtopic.php?t=3499>
Zaidy036
2020-05-24 21:21:41 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
Look at <https://www.dostips.com/forum/viewtopic.php?t=3499>
Robert Prins
2020-05-25 21:01:48 UTC
Permalink
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
If all you have is a hammer, everything looks like a nail!

Use REXX, available for about every OS, it makes the solution almost trivial.

Robert
--
Robert AH Prins
robert(a)prino(d)org
The hitchhiking grandfather - https://prino.neocities.org/indez.html
Some REXX code for use on z/OS - https://prino.neocities.org/zOS/zOS-Tools.html
p***@berkeley.edu
2020-05-26 06:30:02 UTC
Permalink
Post by Robert Prins
Post by Tom Del Rosso
The objective is to remove everything except digits 0-9 from a string.
%alpha_substrings% does contain all possible non-numeric substrings
(space separated), so this should remove them and leave only the digits.
set "only_numbers=string"
for %%t in (%alpha_substrings%) do (
call set "only_numbers=%%only_numbers:%%t= %%"
)
But some characters in the non-numeric substrings, like =, don't get
removed because they are never included in %%t.
If FOR/F is used then it includes those characters because they are not
seen as delimiters, but it doesn't spit them out one at a time in
multiple loops.
I'm tempted to use GWBASIC (MS just made it open-source!).
If all you have is a hammer, everything looks like a nail!
Use REXX, available for about every OS, it makes the solution almost trivial.
Robert
--
Robert AH Prins
robert(a)prino(d)org
The hitchhiking grandfather - https://prino.neocities.org/indez.html
Some REXX code for use on z/OS - https://prino.neocities.org/zOS/zOS-Tools.html
Of course REXX is far superior. I have lots of 'utility' programs that I have
written using Regina REXX in Windows. But the point here is to only use what comes 'built-in' with Windows to get the job done.

Phil Robyn
U.C. Berkeley (retired)
d***@gmail.com
2020-05-26 10:09:00 UTC
Permalink
Post by Robert Prins
If all you have is a hammer, everything looks like a nail!
But if you/colleagues/readers/users can be relied upon to have
Windows, then very much that is difficult to write/read in
pure batch can be done much more readily by using CSCRIPT to
call a .VBS or .JS file.

AFAICS, not yet tried, one should be able to compose REGLARXP.JS
such that, in a batch file,

CSCRIPT //nologo REGLARXP.JS SRC "regexp" Sub DST

will always serve, where SRC & DST are environment variable
names, and REGLARXP.JS uses String.replace(Arg2, Arg3); the
RegExp for removing all non-ASCIIdigits would /\D+/g and Sub
is "".

But maybe it cannot work, because, if it did, the technique should already be well-known here.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
d***@gmail.com
2020-05-26 12:08:24 UTC
Permalink
Post by d***@gmail.com
AFAICS, not yet tried, one should be able to compose REGLARXP.JS
such that, in a batch file,
CSCRIPT //nologo REGLARXP.JS SRC "regexp" Sub DST
will always serve, where SRC & DST are environment variable
names, and REGLARXP.JS uses String.replace(Arg2, Arg3); the
RegExp for removing all non-ASCIIdigits would /\D+/g and Sub
is "".
REVISION : so far so good, but "regexp" must be replaced by two
parts, the main expression and the modifiers, to be joined by
new RegExp if using JavaScript.
--
(c) John Stockton, near London, UK. Using Google Groups.
Mail: J.R.""""""""@physics.org - or as Reply-To, if any.
d***@gmail.com
2020-05-26 12:46:07 UTC
Permalink
Post by d***@gmail.com
REVISION : so far so good, but "regexp" must be replaced by two
parts, the main expression and the modifiers, to be joined by
new RegExp if using JavaScript.
BUT : The following writes to the current environment, but the change is lost when the JavaScript ends.

// REGLARXP.JS (c) JRS 2020-05-26+
// Arguments are : SRC Rex Mod Sub DST

WScript.Echo(" REGLARXP.JS has started")

WScript.Echo(" Test .replace : " + "*NO*".replace(/NO/, "YES"))

var Args = WScript.Arguments
WScript.Echo(' Arguments : ' + Args(0) + ', ' +
Args(1) + ', ' + Args(2) + ', ' + Args(3) + ', ' + Args(4))

var WshShell = new ActiveXObject("WScript.Shell")
var objEnv = WshShell.Environment("Process") // was Process

var Src = objEnv(Args(0))
var Reg = Args(1)
var Mod = Args(2)
var Sub = Args(3)
var Dst = Args(4)
WScript.Echo(" REGLARXP.JS got Args(0) : " + Args(0) + " = " + Src)
WScript.Echo(" REGLARXP.JS got Args(1) : " + Reg)
WScript.Echo(" REGLARXP.JS got Args(2) : " + Mod)
WScript.Echo(" REGLARXP.JS got Args(3) : " + Sub)
WScript.Echo(" REGLARXP.JS got Args(4) : " + Dst + " = " + objEnv(Args(4)))

var Out = Src.replace(new RegExp(Reg, Mod), Sub)

WScript.Echo(" Src.replace result is : " + Out)

WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4)))

WScript.Echo(" Write to environment next")

objEnv(Dst) = Out

WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4)))

WScript.Echo(" REGLARXP.JS finishing")

// End REGLARXP.JS


which gives me

C:\Here>call REGLARXP ZIN "\D+" "g" "A" ZOUT
REGLARXP.JS has started
Test .replace : *YES*
Arguments : ZIN, \D+, g, A, ZOUT
REGLARXP.JS got Args(0) : ZIN = 123 fff 456
REGLARXP.JS got Args(1) : \D+
REGLARXP.JS got Args(2) : g
REGLARXP.JS got Args(3) : A
REGLARXP.JS got Args(4) : ZOUT = X
Src.replace result is : 123A456
Current ZOUT is X
Write to environment next
Current ZOUT is 123A456
REGLARXP.JS finishing
Current ZOUT is : X


C:\Here>


So, how do I, with default privilege, write to the
environment which SET will now show?
--
(c) John Stockton, near London, UK. Using Google Groups.
Mail: J.R.""""""""@physics.org - or as Reply-To, if any.
d***@gmail.com
2020-05-26 16:41:39 UTC
Permalink
Post by d***@gmail.com
So, how do I, with default privilege, write to the
environment which SET will now show?
There MUST be a better way, but the following does it.

MTR is MiniTrue (which I use very frequently), a lister, finder,
and replacer. Here, it just finds the line containing 'SET'.
The idea is to create a small batch file, presently $QQ.BAT,
to do the final move. Probably, FIND or FINDSTR would serve,
maybe faster; I have yet to try.

$REG.BAT is merely for my own convenience in testing; the
true tool is REGLARXP.BAT, which calls REGLARXP.JS and
then completes the job.

Obviously, $QQ.BAT ought to be in %TEMP%, but I'm not sure
whether it will work there.

Some tidying is still needed.

Beware possible line-wrap.


:: $REG.BAT - tester for REGLARXP.BAT

@set ZIN=123 fff 456
@set ZOUT=X

@echo $REG.BAT says ZOUT is : %ZOUT%

@call REGLARXP ZIN "\D+" "g" "A" ZOUT

@echo $REG.BAT says ZOUT is : %ZOUT%

@echo.


:: REGLARXP.BAT (c) J R Stockton 2020-05-26+

@echo REGLARXP.BAT says %5 = %ZOUT%

@call CSCRIPT //nologo REGLARXP.JS %* | MTR -oc- SET > $QQ.BAT

@call $QQ.BAT

@del $QQ.BAT

@echo REGLARXP.BAT says %5 = %ZOUT%

@REM which seems to be true.



// REGLARXP.JS (c) JRS 2020-05-26+
// Arguments are : SRC Rex Mod Sub DST

WScript.Echo(" REGLARXP.JS has started")

WScript.Echo(" Test .replace : " + "*NO*".replace(/NO/, "YES"))

var Args = WScript.Arguments
WScript.Echo(' Arguments : ' + Args(0) + ', ' +
Args(1) + ', ' + Args(2) + ', ' + Args(3) + ', ' + Args(4))

var WshShell = new ActiveXObject("WScript.Shell")
var objEnv = WshShell.Environment("Process") // was Process

var Src = objEnv(Args(0))
var Reg = Args(1)
var Mod = Args(2)
var Sub = Args(3)
var Dst = Args(4)
WScript.Echo(" REGLARXP.JS got Args(0) : " + Args(0) + " = " + Src)
WScript.Echo(" REGLARXP.JS got Args(1) : " + Reg)
WScript.Echo(" REGLARXP.JS got Args(2) : " + Mod)
WScript.Echo(" REGLARXP.JS got Args(3) : " + Sub)
WScript.Echo(" REGLARXP.JS got Args(4) : " + Dst + " = " + objEnv(Args(4)))

var Out = Src.replace(new RegExp(Reg, Mod), Sub)

WScript.Echo(" Src.replace : result is : " + Out)

WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4)))

WScript.Echo(" Write to environment next")

objEnv(Dst) = Out // Does not affect caller environment

WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4))) // looks OK
WScript.Echo(" but set only locally\r\n \t\t\***@SET " + Dst + "=" + Out)

WScript.Echo(" REGLARXP.JS finishing")

// End REGLARXP.JS




There MUST be a better way, not needing $QQ.BAT.

--
(c) John Stockton, near London, UK. Using Google Groups.
Mail: J.R.""""""""@physics.org - or as Reply-To, if any.
Zaidy036
2020-05-26 18:27:07 UTC
Permalink
Post by d***@gmail.com
Post by d***@gmail.com
So, how do I, with default privilege, write to the
environment which SET will now show?
There MUST be a better way, but the following does it.
MTR is MiniTrue (which I use very frequently), a lister, finder,
and replacer. Here, it just finds the line containing 'SET'.
The idea is to create a small batch file, presently $QQ.BAT,
to do the final move. Probably, FIND or FINDSTR would serve,
maybe faster; I have yet to try.
$REG.BAT is merely for my own convenience in testing; the
true tool is REGLARXP.BAT, which calls REGLARXP.JS and
then completes the job.
Obviously, $QQ.BAT ought to be in %TEMP%, but I'm not sure
whether it will work there.
Some tidying is still needed.
Beware possible line-wrap.
:: $REG.BAT - tester for REGLARXP.BAT
@set ZIN=123 fff 456
@set ZOUT=X
@echo $REG.BAT says ZOUT is : %ZOUT%
@call REGLARXP ZIN "\D+" "g" "A" ZOUT
@echo $REG.BAT says ZOUT is : %ZOUT%
@echo.
:: REGLARXP.BAT (c) J R Stockton 2020-05-26+
@echo REGLARXP.BAT says %5 = %ZOUT%
@call CSCRIPT //nologo REGLARXP.JS %* | MTR -oc- SET > $QQ.BAT
@call $QQ.BAT
@del $QQ.BAT
@echo REGLARXP.BAT says %5 = %ZOUT%
@REM which seems to be true.
// REGLARXP.JS (c) JRS 2020-05-26+
// Arguments are : SRC Rex Mod Sub DST
WScript.Echo(" REGLARXP.JS has started")
WScript.Echo(" Test .replace : " + "*NO*".replace(/NO/, "YES"))
var Args = WScript.Arguments
WScript.Echo(' Arguments : ' + Args(0) + ', ' +
Args(1) + ', ' + Args(2) + ', ' + Args(3) + ', ' + Args(4))
var WshShell = new ActiveXObject("WScript.Shell")
var objEnv = WshShell.Environment("Process") // was Process
var Src = objEnv(Args(0))
var Reg = Args(1)
var Mod = Args(2)
var Sub = Args(3)
var Dst = Args(4)
WScript.Echo(" REGLARXP.JS got Args(0) : " + Args(0) + " = " + Src)
WScript.Echo(" REGLARXP.JS got Args(1) : " + Reg)
WScript.Echo(" REGLARXP.JS got Args(2) : " + Mod)
WScript.Echo(" REGLARXP.JS got Args(3) : " + Sub)
WScript.Echo(" REGLARXP.JS got Args(4) : " + Dst + " = " + objEnv(Args(4)))
var Out = Src.replace(new RegExp(Reg, Mod), Sub)
WScript.Echo(" Src.replace : result is : " + Out)
WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4)))
WScript.Echo(" Write to environment next")
objEnv(Dst) = Out // Does not affect caller environment
WScript.Echo(" Current " + Dst + " is " + objEnv(Args(4))) // looks OK
WScript.Echo(" REGLARXP.JS finishing")
// End REGLARXP.JS
There MUST be a better way, not needing $QQ.BAT.
--
(c) John Stockton, near London, UK. Using Google Groups.
SETx _VAR=rtfgh

You can use _VAR in a new batch AFTER close batch SETx was used in.
d***@gmail.com
2020-05-27 16:37:44 UTC
Permalink
It's not good to give a time when the offset from GMT is not known;
and it's good to not use a date format which is ambiguous for
132 days per year.
Post by Zaidy036
Post by d***@gmail.com
Post by d***@gmail.com
So, how do I, with default privilege, write to the
environment which SET will now show?
MTR is MiniTrue (which I use very frequently), a lister, finder,
and replacer. Here, it just finds the line containing 'SET'.
The idea is to create a small batch file, presently $QQ.BAT,
to do the final move. Probably, FIND or FINDSTR would serve,
maybe faster; I have yet to try.
FIND does serve; and I see a probable way of not needing it.
Post by Zaidy036
Post by d***@gmail.com
Obviously, $QQ.BAT ought to be in %TEMP%, but I'm not sure
whether it will work there.
It does work there, and is now $RHUBARB.BAT
Post by Zaidy036
Post by d***@gmail.com
There MUST be a better way, not needing $QQ.BAT.
SETx _VAR=rtfgh
You can use _VAR in a new batch AFTER close batch SETx was used in.
That makes it not well-suited to what the OP seems to want.


The FIND is no longer needed; the temporary file $RHUBARB.BAT is longer.

I would like to get, within the *.JS script, the number of arguments
that it received.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
Loading...