Skip to content
Prev 180305 / 398502 Next

Searching within a ch. string

RON70 wrote:
if you have just one integer number represented in a single string,
here's one way go:

    strings = c('foo 1', '2 bar', 'and 3 as well')
    (numbers = gsub('.*([0-9]+).*', '\\1', strings))

you will need to modify the regex if other sorts of numbers are encoded.
if more than one number is to be extracted from a single string, you can
try the package gsubfn:

    strings = c('foo 1', '2 bar', 'and 3 and 4, too')
    strapply(strings, '[0-9]+')

vQ