I want to replace each character in a string matching a specific pattern. Take the following line
mystring <- c("000450")
I want to match all single zeros to the first non-zero element. I tried something like
gsub("^0[^1-9]*", "x", mystring) [1] "x450"
This expression replaces all leading zeros with one 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?
regex r
RudiSophieson
source share