Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using regexp to remove non-numeric chars from a string

986 views
Skip to first unread message

Thomas Singleton [Eidos]

unread,
Jun 5, 2000, 3:00:00 AM6/5/00
to
Hello,
I'm looking for a example of regexp removing every non-numeric (keeping only
[0-9]) from a string and putting it in a new string.
I've tried different things and looked online for this but couldn't find
anything and for now i had to resort to a for loop which checks every char
and only keeps it when it's good
Also i'd like to know how to access the Tcl manual under linux, it asks me
for a page number
Thanx
Thomas Singleton

Dan Kuchler

unread,
Jun 5, 2000, 3:00:00 AM6/5/00
to Thomas Singleton [Eidos]
"Thomas Singleton [Eidos]" wrote:
>
> Hello,
> I'm looking for a example of regexp removing every non-numeric (keeping only
> [0-9]) from a string and putting it in a new string.
> I've tried different things and looked online for this but couldn't find
> anything and for now i had to resort to a for loop which checks every char
> and only keeps it when it's good

% info patchlevel
8.3.1
% join [regexp -all -inline {[^0-9]+} "abcd12efghi34j5k6l789mnop0"] {}
abcdefghijklmnop

The 'regexp -all -inline' will return a tcl list of all substrings that don't
contain [0-9]. Then the resulting list is joined together into a string.

There are many ways to do this in tcl, and this is just one of them.

> Also i'd like to know how to access the Tcl manual under linux, it asks me
> for a page number

When you are looking for tcl or tk commands do:

man n clock

or

man n list

If your MANPATH is set up right (so that the man pages are found in the man path) then
this should work for all of the tcl and tk commands.

The C library commands are found in section 3:

man 3 Tcl_NewObj

--Dan

Jeffrey Hobbs

unread,
Jun 5, 2000, 3:00:00 AM6/5/00
to Thomas Singleton [Eidos]
"Thomas Singleton [Eidos]" wrote:
> I'm looking for a example of regexp removing every non-numeric (keeping only
> [0-9]) from a string and putting it in a new string.

regsub -all {[^0-9]+} $string {} newstring

--
Jeffrey Hobbs The Tcl Guy
hobbs at ajubasolutions.com Ajuba Solutions (née Scriptics)

Phil Ehrens

unread,
Jun 5, 2000, 3:00:00 AM6/5/00
to
Thomas Singleton [Eidos] wrote:
>I'm looking for a example of regexp removing every non-numeric (keeping only
>[0-9]) from a string and putting it in a new string.

The first one puts all the digits in string2
The second puts all the non-digits there

regsub -all -- {[^\d]+} $string1 {} string2
regsub -all -- {\d+} $string1 {} string2

0 new messages