stringr 包中的 Perl 正则表达式

编程入门 行业动态 更新时间:2024-10-23 22:37:41
本文介绍了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 正则表达式

本文发布于:2023-06-05 19:40:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/526439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包中   正则表达式   stringr   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!