52

I have an R string, with the format

s = `"[some letters and numbers]_[a number]_[more numbers, letters, punctuation, etc, anything]"`

I simply want a way of checking if s contains "_2" in the first position. In other words, after the first _ symbol, is the single number a "2"? How do I do this in R?

I'm assuming I need some complicated regex expresion?

Examples:

39820432_2_349802j_32hfh = TRUE

43lda821_9_428fj_2f = FALSE (notice there is a _2 there, but not in the right spot)

Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
StanLe
  • 5,037
  • 9
  • 38
  • 41

2 Answers2

66
> grepl("^[^_]+_1",s)
[1] FALSE
> grepl("^[^_]+_2",s)
[1] TRUE

basically, look for everything at the beginning except _, and then the _2.

+1 to @Ananda_Mahto for suggesting grepl instead of grep.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • Is there a general solution? One that works when I don't know the string I'm searching for ahead of time, and allows it to have regexp characters inside of it? – Joshua Cheek Oct 15 '16 at 14:32
  • NVM, I figured it out use `fixed=TRUE` option with `grepl`, your answer is good, OP's title is misleading. – Joshua Cheek Oct 15 '16 at 17:57
30

I think it's worth answering the generic question "R - test if string contains string" here.

For that, use the grep function.

# example:
> if(length(grep("ab","aacd"))>0) print("found") else print("Not found")
[1] "Not found"
> if(length(grep("ab","abcd"))>0) print("found") else print("Not found")
[1] "found"
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136