Message-ID: <20101215202051.GA3538@cs.cas.cz>
Date: 2010-12-15T20:20:51Z
From: Petr Savicky
Subject: Numbers in a string
In-Reply-To: <3BE2481A-8B21-4D9E-B3C3-86A549F927D3@comcast.net>
On Wed, Dec 15, 2010 at 01:29:16PM -0500, David Winsemius wrote:
>
> On Dec 15, 2010, at 6:01 AM, Nick Sabbe wrote:
>
> >Hi Felipe,
> >
> >gsub("[^0123456789]", "", "AB15E9SDF654VKBN?dvb.65")
> >results in "15965465".
> >Would that be what you are looking for?
>
>
> I tried figuring out how to do this from a more positive perspective,
> meaning finding a regular expression function that did not require
> negating the desired elements, but the best I could do was make a
> function that accepted a pattern and then hid the underlying negation:
>
> > pullchar <- function(txt, patt){
> if(grepl("\\[", patt)){pattn <- sub("\\[", "\\[\\^", patt)}
> else{
> pattn<- paste("[^",patt,"]", sep="")}
> gsub(pattn, "", txt) #return }
>
> > pullchar("AB15E9SDF654VKBN?dvb.65", "ABD")
> [1] "ABDB"
> > pullchar("AB15E9SDF654VKBN?dvb.65", "[A-Z]")
> [1] "ABESDFVKBN"
> > pullchar("AB15E9SDF654VKBN?dvb.65", "[0-9]")
> [1] "15965465"
>
> Still learning regex so if there is a "positive" strategy I'm all
> ears. ...er, eyes?
One of the suggestions in this thread was to use an external program.
A possible solution without negation in Perl is
@a = ("AB15E9SDF654VKBN?dvb.65" =~ m/[0-9]/g);
print @a, "\n";
15965465
or
@a = ("AB15E9SDF654VKBN?dvb.65" =~ m/[.0-9]+/g);
print join(" ", @a), "\n";
15 9 654 .65
Do you mean something in this direction?
Petr Savicky.