1

I would like to match for the entire word. If you see below, I want the grep for "is" to show only 4 in the output since only "this is a line", contains the entire word "is"

I also tried searching using ^ and $ as shown below, but I'm looking for a string which exactly matches "is". It may contains other words also, as long as the word "is" is present as a separate word

> lines = c("this","this","this","this is a line")
> grep("is",lines)
[1] 1 2 3 4
> grep("^is$",lines)
integer(0)
Cyrus
  • 84,225
  • 14
  • 89
  • 153
tubby
  • 2,074
  • 3
  • 33
  • 55

1 Answers1

5

You want to place word boundaries around your pattern.

grep('\\bis\\b', lines)

Note: The ^ anchor asserts that the regular expression engine's current position in the string is the beginning of the string and $ asserts the position at the end of the string. So, by implementing both anchors, you're telling the engine that the whole string should match.

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • thanks. But if my search key is a variable, say mykey = "is", how do I then use the same? – tubby Apr 10 '15 at 05:03
  • 2
    using paste to concatenate each of the individual components worked for me like grep(paste0("\\b",mykey,"\\b"), lines) Thanks for helping me. – tubby Apr 10 '15 at 10:55