stringr 包中的 Perl 正则表达式 [英] Perl regular expressions in the stringr package

查看:32
本文介绍了stringr 包中的 Perl 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

perl() 函数在 stringr 的最新版本中被弃用,取而代之的是 regex().但是,我似乎无法复制早期的行为.

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")

但是,这不再有效:

str_replace(name, regex("^(.)"), "\\U\\1")

但是使用基础 R 是可行的:

But using base R works:

gsub("^(.)", "\\U\\1", name, perl=TRUE)

还有办法用 stringr 包做到这一点吗?

Is there still a way to do this with the stringr package?

推荐答案

stringr 现在由 stringi 而不是使用 ICU 正则表达式.如果你想实现PCRE,只需在开启perl = TRUE模式时直接使用sub...

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"

这篇关于stringr 包中的 Perl 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆