In order to make a few corrections to a .tex file generated by Bookdown, I need to replace occurrences of `}{` with `,` when it is used in a citation, i.e.

```
s <- "Text.\\autocites{REF1}{REF2}{REF3}. More text \\autocites{REF4}{REF5} and \\begin{tabular}{ll}"

```

Should become

```
"Text.\\autocites{REF1,REF2,REF3}. More text \\autocites{REF4,REF5} and \\begin{tabular}{ll}

```

Because I need to keep the references I tried to look into backreferences, but I cannot seem to get it right, because the number of groups to match is unknown beforehand. Also, I cannot do `stringr::str_replace_all(s, "\\}\\{", ",")`, because `}{` occurs in other places in the document as well.

My best approach so far, is to use a look-behind to only do the replace when the occurence is after `\\autocites`, but then I cannot get the backreferences and grouping right:

```
stringr::str_replace_all(s, "(?<=\\\\autocites\\{)([:alnum:]+)(\\}\\{)", "\\1,")
[1] "Text.\\autocites{REF1,REF2}{REF3}. More text \\autocites{REF4,REF5} and \\begin{tabular}{ll}"

stringr::str_replace_all(s, "(?<=\\\\autocites\\{)([:alnum:]+)((\\}\\{)([:alnum:]+))*", "\\1,\\4")
[1] "Text.\\autocites{REF1,REF3}. More text \\autocites{REF4,REF5} and \\begin{tabular}{ll}"

```

I might be missing some completely obvious approach, so I hope someone can help.