+ static regmatch_t *pmatch = NULL;
+ static int nmatch = 0;
+ int pos = 0;
+
+ if (m_strisempty(s))
+ return 0;
+
+ for (; l; l = l->next) {
+ if (l->nmatch > nmatch) {
+ p_realloc(&pmatch, l->nmatch);
+ nmatch = l->nmatch;
+ }
+
+ if (regexec(l->rx, s, l->nmatch, pmatch, 0) == 0) {
+ /* Copy template into dst, with substitutions. */
+ const char *p = l->tpl, *q;
+
+ for (q = strchr(p, '%'); q; q = strchr(p + 1, '%')) {
+ int n;
+
+ pos += m_strncpy(dst + pos, dlen - pos, p, q - p);
+
+ if (!isdigit((unsigned char)q[1])) {
+ p = q + (q[1] == '%');
+ continue;
+ }