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.
CodePudding user response:
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 insteadfixed=TRUE
, except that we want to be able to match on arbitrary numbers in[i]
.