2

I have to do a regexp replacement with more than 9 capture groups. How can I do it?

Here there is my code (with \\10 and \\12 that they do not work as I expected to):

(perform-replace "\\\\href{https://doi.org/\\(.*\\)}{\\\\emph{\\(JHEP\\|JCAP\\)}\\([
  ]*\\)?{\\\\bfseries\\([
  ]*\\)?\\([0-9]+\\)}\\([
  ]*\\)?(\\([0-9]\\)\\([0-9]\\)\\([0-9]\\)\\([0-9]\\))\\([
  ]*\\)?\\([0-9]+\\)}" "\\\\href{https://doi.org/\\1}{\\\\emph{\\2} {\\\\bfseries \\9\\10\\5} (\\7\\8\\9\\10) \\12}" t t nil 1 nil a z)
  • 1
    The question is interesting per se. Emacs regexp only allow digits from 1 to 9 after the backslash for referencing. But you do not need all groups of the search string in the replacement. Therefore you can change some of the groups in your search string into shy groups (?:...). Afterwards you only really need 8 captured groups. – Tobias May 7 '18 at 10:34
  • My assumption is that having more than 9 capture groups wouldn't be trivial, as syntax for matched groups probably was designed with just a single digit for the group in mind. Doing this sort of thing would probably require writing a parser by hand. You should be able to capture multiple digits wrapped into parens with a single group as well. – DoMiNeLa10 May 7 '18 at 10:37
  • 2
    Here is an example with twenty-three (23) regex groups: stackoverflow.com/a/20960301/2112489 . I have increased it in my own setup to 35 regex groups, but I would imagine that 23 is sufficient for you to see how it might be done. To experiment with a regexp, you may enjoy typing the text to be matched in a scratch buffer and then type M-x re-builder -- the matches show up with color-codes in the scratch buffer and you can play around with changing the regex until you get the desired result. – lawlist May 7 '18 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.