Murali
Murali

Reputation: 11

R, grepl, searching for a substring

This might look like a repeat question but I've searched Stackoverflow for hours and I've tried all that I could find but this wasn't solved. I have a character array with .xlsx files and I need to remove the ones with ~$ at the beginning of the file name, e.g.

~$MS_LM CASH.xlsx

But grepl returns false even for grepl(ch[1],"MS"), let alone special characters.

Upvotes: 1

Views: 671

Answers (1)

akrun
akrun

Reputation: 887078

The $ is a regex metacharacter and needs to be escaped with backslash to use it literally in a pattern with grepl():

grepl("\\$MS", ch[1])

Upvotes: 1

Related Questions