Going by the Emacs source code, it is absolutely possible to use more than 9 regex capture groups:
/* Since we have one byte reserved for the register number argument to
{start,stop}_memory, the maximum number of groups we can report
things about is what fits in that byte. */
#define MAX_REGNUM 255
/* But patterns can have more than 'MAX_REGNUM' registers. Just
ignore the excess. */
typedef int regnum_t;
However you've run into a different limitation, a maximum of 9 backreferences:
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
{
regnum_t reg = c - '0';
if (reg > bufp->re_nsub || reg < 1
/* Can't back reference to a subexp before its end. */
|| group_in_compile_stack (compile_stack, reg))
FREE_STACK_RETURN (REG_ESUBREG);
laststart = b;
BUF_PUSH_2 (duplicate, reg);
}
break;
It is possible to overcome this limitation by using Lisp code instead of a replacement pattern:
(defvar big-rx
(rx (group (+ "x")) " " (group (+ "x")) " "
(group (+ "x")) " " (group (+ "x")) " "
(group (+ "x")) " " (group (+ "x")) " "
(group (+ "x")) " " (group (+ "x")) " "
(group (+ "x")) " " (group (+ "x")) " "
(group (+ "x"))))
(let ((text (mapconcat 'identity (mapcar (lambda (x) (make-string x ?x))
(number-sequence 1 11))
" ")))
(when (string-match big-rx text)
(setq text (replace-match (make-string 1 ?y) t t text 1))
(setq text (replace-match (make-string 11 ?y) t t text 11))))
;; => "y xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx xxxxxxxxxx yyyyyyyyyyy"
The above example uses eleven capture groups and replaces the first and eleventh one.
(?:...)
. Afterwards you only really need 8 captured groups. – Tobias May 07 '18 at 10:34M-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 07 '18 at 14:05