RobustTurd
RobustTurd

Reputation: 93

Regular expression to match string ending with letter but NOT ending with a phrase in R

I need to use grep to find all strings ending with G but NOT ending with STIG or VÄG in the following character vector:

test=c("EWASPG","AVOPW","SDAGSTIG","ASDVVÄG","ASDVWE","QSCIUVG","QWSFNJG")

I tried this but it returns false for any string with the letters S, T , I, V, Ä preceding the G instead of returning false when the G is preceded by the exact phrase.

grep("[^((STI)|(VÄ))]G$", test, value=T)

# [1] "EWASPG"  "QWSFNJG"

Thanks!

I am aware of this post.

Upvotes: 2

Views: 1433

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336428

A character class always matches a single character, so [^(STI)] would match any character except (, S, T, I or ).

You can use a negative lookbehind assertion to make sure that the string doesn't end in a certain substring, but you need to enable Perl-compatible regex mode in R:

grep("(?<!STI|VÄ)G$", test, perl=TRUE, value=TRUE);

Test it live on regex101.com.

Upvotes: 4

Related Questions