4

I would like to insert a colon every five characters starting from the end of the string, preferably using regex and gsub in R.

text <- "My Very Enthusiastic Mother Just Served Us Noodles!"

I have been able to insert a colon every five characters from beginning of the text using:

gsub('(.{5})', "\\1:", text, perl = T)

I have written an inelegant function for achieving this as follows:

library(dplyr)
str_reverse<-function(x){
  strsplit(x,split='')[[1]] %>% rev() %>% paste(collapse = "") 
}

text2<-str_reverse(text)
text3<-gsub('(.{5})', "\\1:", text2, perl = T)
str_reverse(text3)

to get the desired result

[1] "M:y Ver:y Ent:husia:stic :Mothe:r Jus:t Ser:ved U:s Noo:dles!"

Is there a way this can be achieved directly using regular expressions?

Sotos
  • 51,121
  • 6
  • 32
  • 66
Joseph Kigotho
  • 179
  • 1
  • 14
  • Consider that the `stringi` package has the `stri_reverse` function already available and super efficient. – nicola Nov 16 '18 at 12:45

1 Answers1

7

You may use

gsub('(?=(?:.{5})+$)', ":", text, perl = TRUE)
## => [1] "M:y Ver:y Ent:husia:stic :Mothe:r Jus:t Ser:ved U:s Noo:dles!"

See the regex demo

The (?=(?:.{5})+$) pattern matches any location inside the string that is followed with any 5 chars (other than line break chars) 1 or more times up to the end of the string.

If the input string can contain line breaks you need to add (?s) at the start of the pattern (since . in PCRE regex does not match line breaks by default):

'(?s)(?=(?:.{5})+$)'
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    @AndreElrico 1) It does not matter if you use lazy or greedy quantifiers here, `(?=(?:.{5})+?$)` = `(?=(?:.{5})+$)` because the next pattern is `$`, end of string and `.{5}` goes matching up to the end of the string anyway, 2) since the regex searches for matches from left to right, the 2nd position is matched, not the 5 from the end. PCRE regex cannot be set to search from the end. – Wiktor Stribiżew Nov 16 '18 at 12:46
  • thanks you also found this here https://www.rexegg.com/regex-quantifiers.html#longest_shortest . this was a missing piece in my brain – Andre Elrico Nov 16 '18 at 12:49
  • @AndreElrico You may want to read [my quantfier explanations](https://stackoverflow.com/a/33869801/3832970), too. – Wiktor Stribiżew Nov 16 '18 at 12:53