Skip to content
Prev 245192 / 398503 Next

Numbers in a string

> On Wed, Dec 15, 2010 at 11:08:06AM -0200, Henrique
> Dallazuanna wrote:
>> Try this:
    >> 
    >> gsub("[^0-9]", "", "AB15E9SDF654VKBN?dvb.65")

    > Consider also

    >   strsplit("AB15E9SDF654VKBN?dvb.65", "[^.0-9][^.0-9]*")
    > [[1]] [1] "" "15" "9" "654" ".65"

which can be abbreviated to 

       strsplit("AB15E9SDF654VKBN?dvb.65", "[^.0-9]+")

Note: 
 R's regular expression matching capability is really very close to Perl's
 and in those cases it is not, these functions have an argument
 'perl' (default FALSE) you can switch on.

Martin


    >> On Wed, Dec 15, 2010 at 6:55 AM, Luis Felipe Parra <
>> felipe.parra at quantil.com.co> wrote:
>> 
    >> > 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

    > ______________________________________________
    > 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.