问题
The perl()
function is deprecated in the latest version of stringr in favor of regex()
. However, I don't seem to be able to replicate the earlier behavior.
To capitalize the first letter of a vector of strings, this used to work:
name <- c("jim", "john", "bill")
str_replace(name, perl("^(.)"), "\\U\\1")
However, this no longer works:
str_replace(name, regex("^(.)"), "\\U\\1")
But using base R works:
gsub("^(.)", "\\U\\1", name, perl=TRUE)
Is there still a way to do this with the stringr package?
回答1:
stringr is now powered by stringi instead which uses ICU regular expressions. If you want to implement PCRE, simply use sub
directly while turning on perl = TRUE
mode ...
sub('^(.)', '\\U\\1', name, perl=TRUE)
[1] "Jim" "John" "Bill"
来源:https://stackoverflow.com/questions/30214528/perl-regular-expressions-in-the-stringr-package