# Can I apply a face to an optional group?

When I try to execute the following, font-lock fails abruptly after it matches the first group. A little bit of debugging with the neat tool font-lock-studio and I realized that font-lock is upset that it can't find group 2. How can I tell it not to freak out?

(define-derived-mode expl3-mode prog-mode
"expl3"
"Mode for editing expl3 source code."

(setq font-lock-set-defaults nil)
(font-lock-set-defaults)
nil
((,(rx (* whitespace)
(group (+ (any alnum "-")))
(* whitespace)
(optional "/" (* whitespace)
(group (+ (any alnum "_"))))      ; note
(+ whitespace)
(group "." (+ (any alpha)) (optional "_"))
(group (optional (any alpha) (+ (any alpha "_"))))
(group ":" (* (any "nNpTFwcVvxofD"))))
(1 font-lock-variable-name-face t)
(2 font-lock-variable-name-face t)               ; the optional group
(3 font-lock-type-face t)
(4 font-lock-function-name-face t)
(5 font-lock-keyword-face t)))))


### Test string

test-key .tl_set:N ...
with / grouping .code:n ...

• It looks like a typo in the regexp, try: (optional "/" (* whitespace) (group (+ (any alnum "_")))). – Lindydancer Apr 5 '15 at 18:36
• @Lindydancer Doh! :) That was it precisely. – Sean Allred Apr 5 '15 at 18:40

Looks like you can use the LAXMATCH flag for the second group:
(2 font-lock-variable-name-face t t)

• If you step in font-lock-studio, you'll see that the with /part simply isn't included in the match at all (you'll see this as it's not highlighted with a background color like the rest of the line is). I would revise the regexp to ensure that it matches, place the point at the beginning of when, use looking-at` and the beginning of the regexp, then add more and more of it until it longer returns true, then you know which part of the regexp caused it to fail to match. – Lindydancer Apr 5 '15 at 18:23