I'm trying to use a regular expression of the form "foo\\(?:\\(bar\\)\\(baz\\)\\)*\\(quux\\)", but I want the inner two subexpressions to retain all of their matches, not just the first one. I realize this may be impossible to achieve as stated, so I'll give more detail about why I want this to work.

My specific case is that I'm trying produce a major mode for editing and highlighting Dwarf Fortress "raw" files. The regular expression I'm having this trouble with is


, which successfully matches a single complete raw tag with all of its parameters; however, capturing groups 3 and 4 only end up containing the last parameter (after having successfully matched all the previous ones), and therefore all but the last parameter is not highlighted by font-lock. I understand why this is the case, but I'd like to know if there is a way to get around it, or to otherwise enable my desired behavior.

  • 1
    Is "foo\\(?:bar\\|baz\\)*\\(quux\\)" what you want? Note the or-operator \\|. – Tobias Feb 9 '16 at 10:32
  • @Tobias No; I need it to match things of the pattern foo bar baz bar baz … bar baz quux and highlight all the bar baz pairs in between. – Ptharien's Flame Feb 9 '16 at 15:29

You can use an anchored font-lock rule. They are used when you want a search within a search. The look like:


In your case, it could be something like:

(defvar my-raw-rule
      ;; Pre-match form: Limit search to closing "]" or end-of-line.
        (if (re-search-forward "\\]" (line-end-position) t)
      ;; Post-match form
      (0 font-lock-constant-face)))))

Note that the rule highlights words after the [ even when the ] isn't present -- this is typically what you want when writing code.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.