问题描述
我有一列如下.
9453、55489、4588、18893、4457、2339、45489HQ,7833HQ
9453, 55489, 4588, 18893, 4457, 2339, 45489HQ, 7833HQ
如果数字少于5位,我想添加前导零.但是,有些数字最后带有"HQ",有些则没有.(我检查了其他帖子,它们在"HQ"部分没有类似的问题)
I would like to add leading zero if the number is less than 5 digits. However, some numbers have "HQ" in the end, some don't.(I did check other posts, they dont have similar problem in the "HQ" part)
所以最终所需的输出应该是:
so the finally desired output should be:
09453、55489、04588、18893、04457、02339、45489HQ,07833HQ
09453, 55489, 04588, 18893, 04457, 02339, 45489HQ, 07833HQ
您知道如何执行此操作吗?非常感谢您阅读我的帖子!
any idea how to do this? Thank you so much for reading my post!
推荐答案
使用正则表达式的单行代码:
A one-liner using regular expressions:
my_strings <- c("9453", "55489", "4588",
"18893", "4457", "2339", "45489HQ", "7833HQ")
gsub("^([0-9]{1,4})(HQ|$)", "0\\1\\2",my_strings)
[1] "09453" "55489" "04588" "18893"
"04457" "02339" "45489HQ" "07833HQ"
说明:
^ start of string
[0-9]{1,4} one to four numbers in a row
(HQ|$) the string "HQ" or the end of the string
括号表示顺序的捕获组.因此,0\\1\\2
表示0
,后跟第一个捕获组[0-9]{1,4}
和第二个捕获组HQ|$
.
Parentheses represent capture groups in order. So 0\\1\\2
means 0
followed by the first capture group [0-9]{1,4}
and the second capture group HQ|$
.
当然,如果有5个数字,则表示正则表达式不匹配,因此不会更改.
Of course if there is 5 numbers, then the regex isn't matched, so it doesn't change.
这篇关于R-在字符串中添加前导零,没有固定的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!