I have multiple strings that are similar to the following pattern:
dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0")
I need to change all 0 values to "." before the first character value within a string. My desired output in this example would be:
"........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0".
I tried using gsub to accomplish this task:
gsub("\\G([^_\\d]*)\\d", ".\\1", dat, perl=T)
Unfortunately it changed all of the 0s to "." instead of the 0s preceding the first "A".
Can someone please help me with this issue?
If you wish to simply replace each leading
0with a., you can useHere,
\G0matches a0char at the start of string, and then every time after a successful match. See this regex demo.If you need to replace each
0in a string before the first letter you can useHere,
\Gmatches start of string or end of the preceding successful match,[^\p{L}0]*matches zero or more chars other than a letter and0, then\Komits the matched text, and then0matches the0char and it is replaced with a.. See this regex demo.See the R demo online: