Perl regular expressions in the stringr package

断了今生、忘了曾经 提交于 2019-12-10 17:17:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!