gpt4 book ai didi

regex - R Regex-提取5位数字

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

我有一个像这样的字符串a:

stundenwerte_FF_00691_19260101_20131231_hist.zip



并希望从中提取5位数字“00691”。

我尝试使用 gregexprregmatches以及 stringr::str_extract但无法弄清楚正确的rexexp。
我到了:

gregexpr("[:digits{5}:]",a)



哪个应该返回5位数字,我不知道如何解决它。
这不能正常工作:(
m <- gregexpr("[:digits{5}:]",a)
regmatches(a,m)

预先感谢您的帮助!

最佳答案

您可以简单地使用sub来获取数字,对于这种简单情况,不需要IMO regmatches

x <- 'stundenwerte_FF_00691_19260101_20131231_hist.zip'
sub('\\D*(\\d{5}).*', '\\1', x)
# [1] "00691"

编辑:如果您还有其他在前面包含数字的字符串,则可以稍稍修改表达式。
sub('.*_(\\d{5})_.*', '\\1', x)

关于regex - R Regex-提取5位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558764/

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