user2987808
user2987808

Reputation: 1437

Unexpected results when using regex to change case

I'm trying to change the case of a character using regex and the stringr package, but I'm getting a curious result. I would expect both expressions below to give the same result (capitalizing the first character), but only the grep function gives the expected result:

> str_replace("will", "(^\\w)", regex("\\U\\1"))
[1] "1ill"
> gsub("(^\\w)", "\\U\\1", "will", perl = TRUE)
[1] "Will"

Related:

gsub error turning upper to lower case in R

Upvotes: 2

Views: 73

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

gsub uses a kind of a PCRE regex (note PCRE regex does not allow case changing operators \L / \l and \U / \u with \E, but R extends their functionality like in Boost library that supports those operators).

stringr library uses ICU regex library and there is no support for these case changing operators, and the support was not added to the original library functionality.

Upvotes: 2

Related Questions