I want to replace every single character in a string that matches a certain pattern. Take the following string
mystring <- c("000450")
I want to match all single zeros up to the first element that is non-zero. I tried something like
gsub("^0[^1-9]*", "x", mystring)
[1] "x450"
This expression replaces all the leading zeros with a single x
. But instead, I want to replace all three leading zeros with xxx
. The preferred result would be
[1] "xxx450"
Can anyone help me out?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…