Antoine
Antoine

Reputation: 1719

R grepl: whole word matching not working for punctuation marks

text = 'this is ; an example'

Language is R. I'd like to understand why:

grepl("\\<is\\>",text)

returns TRUE

while

grepl("\\<;\\>",text)

returns FALSE

Note that setting the perl argument to TRUE or FALSE doesn't make any difference. I know that grepl(";",text) works, my question is why doesn't it work anymore when we add word boundaries.

Upvotes: 1

Views: 496

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

The \< is a leading word boundary and the \> is a trailing word boundary. So, the char after \< must be a word char, and the char before \> should be a word char.

The ; is not a word char. The \<;\> will never match any string as the \<; means match a ; that is preceded with a leading word boundary and ;\> means match a ; that is followed with a trailing word boundary, i.e. requires a ; to be a word char, which is false.

Upvotes: 1

Related Questions