Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace repeating character with another repeated character

Tags:

regex

r

I would like to replace 3 or more consecutive 0s in a string by consecutive 1s. Example: '1001000001' becomes '1001111111'.

In R, I wrote the following code:

gsub("0{3,}","1",reporting_line_string)

but obviously it replaces the 5 0s by a single 1. How can I get 5 1s ?

Thanks,

like image 212
nassimhddd Avatar asked Feb 12 '23 16:02

nassimhddd


1 Answers

You can use gsubfn function, which you can supply a replacement function to replace the content matched by the regex.

require(gsubfn)
gsubfn("0{3,}", function (x) paste(replicate(nchar(x), "1"), collapse=""), input)

You can replace paste(replicate(nchar(x), "1"), collapse="") with stri_dup("1", nchar(x)) if you have stringi package installed.

Or a more concise solution, as G. Grothendieck suggested in the comment:

gsubfn("0{3,}", ~ gsub(".", 1, x), input)

Alternatively, you can use the following regex in Perl mode to replace:

gsub("(?!\\A)\\G0|(?=0{3,})0", "1", input, perl=TRUE)

It is extensible to any number of consecutive 0 by changing the 0{3,} part.

I personally don't endorse the use of this solution, though, since it is less maintainable.

like image 84
nhahtdh Avatar answered Feb 15 '23 11:02

nhahtdh