regex - Exact string match -


a trivial question - tried few iterations , still couldnt right answer. maybe "grep" not right command or understanding off.

i have string:

"part 2.2 secondary objectives of study". 

i trying exact match on: "secondary objective". thought use "fixed=true" here, matches both.

> str5 = "part 2.2 secondary objectives of study" > line<-grep("secondary objectives",str5,fixed=true) > line [1] 1  > str5 = "part 2.2 secondary objectives of study"  > line<-grep("secondary objective",str5,fixed=true) > line [1] 1 

i understand "grep" doing should. searching string "secondary objective" technically in original string. understanding exact match using "fixed=true' command. mistaken.

if "grep" "fixed=true" not right command exact match,what work? "str_match" did not work either. if pattern is: "secondary objective", should return "integer(0)" if pattern "secondary objectives", should return 1.

any input appreciated. much! - simak


update: trying out arun's suggestion below - works fine is.

 str5 = "part 2.2 secondary objectives of study" > grep("(secondary objectives)(?![[:alpha:]])",str5, perl=true) [1] 1  > grep("(secondary objective)(?![[:alpha:]])",str5, perl=true) integer(0) 

str5 = "part 2.2 secondary objectives of study" grep("(pat)(?![[:alpha:]])",str5, perl=true) integer(0)

however when did this:  > str5 = "part 2.2 secondary objectives of study" > pat <- "secondary objectives" > grep("(pat)(?![[:alpha:]])",str5, perl=true) integer(0)  thought can call "pat" inside "grep". incorrect? thanks! 

one way think of use negative lookahead (with perl=true option). is, check if there no other alphabet after pattern , if 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 if pattern searching @ end because search that's not alphabet. is,

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

Comments