Note that there are some explanatory texts on larger screens.

plurals
  1. POExact string match
    text
    copied!<p>A very trivial question - tried a few iterations and still couldnt get the right answer. Maybe "grep" is not the right command or my understanding is completely off.</p> <p>I have a string: </p> <pre><code>"Part 2.2 are Secondary objectives of this study". </code></pre> <p>I am trying to do an exact match on: "Secondary objective". I thought I could use "fixed=TRUE" here, but it matches both.</p> <pre><code>&gt; str5 = "Part 2.2 are Secondary objectives of this study" &gt; line&lt;-grep("Secondary objectives",str5,fixed=TRUE) &gt; line [1] 1 &gt; str5 = "Part 2.2 are Secondary objectives of this study" &gt; line&lt;-grep("Secondary objective",str5,fixed=TRUE) &gt; line [1] 1 </code></pre> <p>I understand that "grep" is doing exactly as it should. It is searching for the string "Secondary objective" which is technically in the original string. But my understanding is I could do an exact match using the "fixed=TRUE' command. But obviously I am mistaken. </p> <p>If "grep" with "fixed=TRUE" is not the right command for exact match,what will work? "str_match" did not work either. If my pattern is: "Secondary objective", it should return "integer(0)" but if my pattern is "Secondary objectives", it should return 1. </p> <p>Any input is greatly appreciated. Thanks much! - simak</p> <hr> <p>Update: Trying out Arun's suggestion below - works fine as is.</p> <pre><code> str5 = "Part 2.2 are Secondary objectives of this study" &gt; grep("(Secondary objectives)(?![[:alpha:]])",str5, perl=TRUE) [1] 1 &gt; grep("(Secondary objective)(?![[:alpha:]])",str5, perl=TRUE) integer(0) </code></pre> <blockquote> <p>str5 = "Part 2.2 are Secondary objectives of this study" grep("(pat)(?![[:alpha:]])",str5, perl=TRUE) integer(0)</p> </blockquote> <pre><code>However when I did this: &gt; str5 = "Part 2.2 are Secondary objectives of this study" &gt; pat &lt;- "Secondary objectives" &gt; grep("(pat)(?![[:alpha:]])",str5, perl=TRUE) integer(0) Thought I can call "pat" inside "grep". Is that incorrect? Thanks! </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload