grep - R: regular expression to specify end of string char is a letter -
string = c("hello-", "helloa", "helloa") grep("hello$[a-z]", string)
i wish find indices of strings in next character after word "hello" letter (case insensitive). code above doesn't work, grep() homecoming indices 2 , 3 since words have letter after "hello"
use positive lookahead
> string = c("hello-", "helloa", "helloa") > grep('hello(?=[a-za-z])', string, perl=t) [1] 2 3
(?=[a-za-z])
positive lookahead asserts character next string hello
must letter.
or
> grep('hello[a-za-z]', string) [1] 2 3
add $
in regex if there 1 letter next string hello
. $
asserts @ end.
> grep('hello[a-za-z]$', string) [1] 2 3 > grep('hello(?=[a-za-z]$)', string, perl=t) [1] 2 3
r grep
No comments:
Post a Comment