[R] Numbers in a string
Petr Savicky
savicky at cs.cas.cz
Wed Dec 15 21:20:51 CET 2010
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.
More information about the R-help
mailing list