Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove 1 character from both sides of a recurring substring where one digit changes in each occurrence

I need to remove apostrophes from both sides of a sub-string. The substring occurs numerous times within a starting string, and one digit changes within the substring for each occurrence.

starting_string = "{'color':'Highcharts.getOptions().colors[0]','color':'Highcharts.getOptions().colors[1]','color':'Highcharts.getOptions().colors[2]'}"

substring = Highcharts.getOptions().colors[i]

desired_string = "{'color':Highcharts.getOptions().colors[0],'color':Highcharts.getOptions().colors[1],'color':Highcharts.getOptions().colors[2]}"

Above, in ‘substring’, ‘i’ represents the digit that changes in each occurrence of the substring.

The number of times ‘substring’ occurs in ‘starting_string’ will vary. This example is simplified.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

gsub("'(Highcharts\\.getOptions\\(\\)\\.colors\\[[0-9]+\\])'",
     "\\1", starting_string)
# [1] "{'color':Highcharts.getOptions().colors[0],'color':Highcharts.getOptions().colors[1],'color':Highcharts.getOptions().colors[2]}"

Explanation of the regex:

  • the parens (Hig...) define a group that we’ll reference later using \\1;
  • the enveloping ' are the literal single quotes; note that these are outside the paren-group, as we will want to drop them once we find them;
  • I took the liberty of inferring that i means "any number", so I replaced it with [0-9]+ which means "one or more digit".
  • many characters have special meaning in regex, so they are backslash-escaped; here, they are (, ), [, ], and .. For the record, I might have been able to omit all of the backslashes and used instead fixed=TRUE, except that we want to be able to match on arbitrary numbers in [i].
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading