BRZ
BRZ

Reputation: 695

Exact string match

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.

I have a string:

"Part 2.2 are Secondary objectives of this study".

I am trying to do an exact match on: "Secondary objective". I thought I could use "fixed=TRUE" here, but it matches both.

> str5 = "Part 2.2 are Secondary objectives of this study"
> line<-grep("Secondary objectives",str5,fixed=TRUE)
> line
[1] 1

> str5 = "Part 2.2 are Secondary objectives of this study" 
> line<-grep("Secondary objective",str5,fixed=TRUE)
> line
[1] 1

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.

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.

Any input is greatly appreciated. Thanks much! - simak


Update: Trying out Arun's suggestion below - works fine as is.

 str5 = "Part 2.2 are Secondary objectives of this study"
> grep("(Secondary objectives)(?![[:alpha:]])",str5, perl=TRUE)
[1] 1

> grep("(Secondary objective)(?![[:alpha:]])",str5, perl=TRUE)
integer(0)

str5 = "Part 2.2 are Secondary objectives of this study" grep("(pat)(?![[:alpha:]])",str5, perl=TRUE) integer(0)

However when I did this:

> str5 = "Part 2.2 are Secondary objectives of this study"
> pat <- "Secondary objectives"
> grep("(pat)(?![[:alpha:]])",str5, perl=TRUE)
integer(0)

Thought I can call "pat" inside "grep". Is that incorrect? Thanks!

Upvotes: 1

Views: 1565

Answers (1)

Arun
Arun

Reputation: 118889

One way I could think of is to use negative lookahead (with perl=TRUE option). That is, we check if there is no other alphabet immediately after your pattern and if so return 1 else no match.

grep("(Secondary objective)(?![[:alpha:]])", x, perl=TRUE)
# integer(0)

grep("(Secondary objectives)(?![[:alpha:]])", x, perl=TRUE)
# [1] 1

This'd work even if the pattern you were searching is at the end because we search for anything that's not an alphabet. That is,

grep("(this stud)(?![[:alpha:]])", x, perl=TRUE)
# integer(0)

grep("(this study)(?![[:alpha:]])", x, perl=TRUE)
# [1] 1

Upvotes: 1

Related Questions