gpt4 book ai didi

R正则表达式从向量中获取年份

转载 作者:行者123 更新时间:2023-12-01 06:12:54 24 4
gpt4 key购买 nike

好吧,正则表达式正式成为我存在的祸根。

我需要一个子表达式来获取年份作为 R 中的一个单独的字符向量:

vector <- c("Hello_world_1999_otherstuff.file", "Hello_2010_world_otherstuff.file", "2015_hello_world_help_me_thanks!.file")

vector.desired <- c("1999", "2010", "2015")

谢谢!

最佳答案

libray(stringr)
str_extract(vector, "\\d{4}")

关于R正则表达式从向量中获取年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48515015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com