gpt4 book ai didi

regex - 从字符串中提取和合并数字

转载 作者:行者123 更新时间:2023-12-02 03:12:15 25 4
gpt4 key购买 nike

我有如下带数字的字符串:

972 2 6424979
81|5264627
49-0202-2801986
07.81.48.27.89
0664/3420034
06041 - 8728

并希望得到这样的输出:

97226424979
815264627
4902022801986
0781482789
06643420034
060418728

我尝试使用:

as.numeric(gsub("([0-9]+).*$", "\\1", numbers))

但数字在输出中是分开的。

最佳答案

要获得准确的输出,

#to avoid scientific notation
options(scipen=999)

#find which have leading 0
ind <- which(substring(x, 1, 1) == 0)

y <- as.numeric(gsub("\\D", "", numbers))
y[ind] <- paste0('0', y[ind])
y
#[1] "97226424979" "815264627" "4902022801986" "0781482789" "06643420034" "060418728"

关于regex - 从字符串中提取和合并数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269449/

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