Skip to content
Prev 245020 / 398503 Next

Numbers in a string

On Dec 15, 2010, at 6:01 AM, Nick Sabbe wrote:

            
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?