正则表达式 – 从R中的字母数字字符中删除前导零
发布时间:2020-12-14 06:27:03 所属栏目:百科 来源:网络整理
导读:我有一个带字母数字字符的字符向量d d - c("012309 template","separate 00340","00045","890 098","3405 garage","matter00908")d[1] "012309 template" "separate 00340" "00045" "890 098" "3405 garage" "matter00908" 如何从R中的所有数字中删除前导零
我有一个带字母数字字符的字符向量d
d <- c("012309 template","separate 00340","00045","890 098","3405 garage","matter00908") d [1] "012309 template" "separate 00340" "00045" "890 098" "3405 garage" "matter00908" 如何从R中的所有数字中删除前导零? 预期输出如下 out <- c("12309 template","seperate 340","45","890 98","matter908") out [1] "12309 template" "seperate 340" "45" "890 98" "3405 garage" "matter908"
你可以使用负向lookbehind来消除0,除非前面有一个数字:
> d <- c("100001","012309 template","matter00908") > gsub("(?<![0-9])0+","",d,perl = TRUE) [1] "100001" "12309 template" "separate 340" "45" [5] "890 98" "3405 garage" "matter908" 另一种使用正则表达式的方法: > gsub("(^|[^0-9])0+","1",perl = TRUE) [1] "100001" "12309 template" "separate 340" "45" [5] "890 98" "3405 garage" "matter908" > (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |