[R] Numbers in a string

David Winsemius dwinsemius at comcast.net
Wed Dec 15 19:29:16 CET 2010


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?

-- 
David.
>
>
> Nick Sabbe
> --
> ping: nick.sabbe at ugent.be
> link: http://biomath.ugent.be
> wink: A1.056, Coupure Links 653, 9000 Gent
> ring: 09/264.59.36
>
> -- Do Not Disapprove
>
>
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org 
> ] On
> Behalf Of Rainer Schuermann
> Sent: woensdag 15 december 2010 11:19
> To: r-help at r-project.org
> Subject: Re: [R] Numbers in a string
>
> If your OS is Linux, you might want to look at sed or gawk. They are  
> very
> good and efficient for such tasks.
> You need it once or as a part of program?
> Some samples would be helpful...
> Rgds,
> Rainer
>
>
> -------- Original-Nachricht --------
>> Datum: Wed, 15 Dec 2010 16:55:26 +0800
>> Von: Luis Felipe Parra <felipe.parra at quantil.com.co>
>> An: r-help <r-help at r-project.org>
>> Betreff: [R] Numbers in a string
>
>> Hello, I have stings which have all sort of characters (numbers,  
>> letters,
>> punctuation marks, etc) I would like to stay only with the numbers in
>> them,
>> does somebody know how to do this?
>>
>> Thank you
>>
>> Felipe Parra
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> -- 
>
> -------
>
> Windows: Just say No.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list