Skip to content

Searching within a ch. string

7 messages · Ron Michael, Luc Villandre, Duncan Murdoch +2 more

#
Hi all, is there any function to find some words in a character-string? For
example suppose the string is : "gdfsa-sdhchc-88", now I want to find
whether this string contains "sdhch". Is there any R function to do that?

Regards,
#
Hi Ron,

Look up the grep() function.

Cheers,
#
On 5/11/2009 10:07 AM, RON70 wrote:
See ?grep.  There are several functions described there, with different 
outputs.

Duncan Murdoch
#
The following is TRUE if the indicated string is found:

regexpr("sdhch", "gdfsa-sdhchc-88") > 0

See ?regexpr

Either of the next two will break up a string into words. The
first needs to know the delimiters whereas the second
needs to know the contents:
[1] "gdfsa"  "sdhchc" "88"
[1] "gdfsa"  "sdhchc" "88"

See ?strplit and ?regex and also the gsubfn home page at:
http://gsubfn.googlecode.com
where you can find info on strapply and also further resources
on regular expressions.
On Mon, May 11, 2009 at 10:07 AM, RON70 <ron_michael70 at yahoo.com> wrote:
2 days later
#
Thanks for these suggestions. However I have one more question. Is there any
way to extract only numbers? For example I want to extract only "88" in my
example.

Regards,
MUHC-Research wrote:

  
    
#
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
#
If the input is "gdfsa-sdhchc99-88" then
assuming you only want "88" but not
"99" then if s is the vector of words
that we already computed:

s[regexpr("^[0-9]+", s) > 0]

or that could be combined with the strapply solution
into one line:
[1] "88"

That picks out words and then tests them as before.
On Thu, May 14, 2009 at 2:58 AM, RON70 <ron_michael70 at yahoo.com> wrote: