jake
jake

Reputation: 351

Reusing capture group inside a regexp

I am writing an emacs major mode for an in house scripting language. I am trying to set up font lock regexps for macros used by a preprocessing tool, which are of the form $(VNAME). I want to match both the entire macro, and any later usages of the variable name. (i.e. both $(VNAME) and VNAME should be matched).

I have successfully matched the entire preprocessor macro with:

...
'("\\$\(.*\)"
  .font-lock-preprocessor-face)
...

I have attempted to then also match the variable name by defining the letters inside the parenthesis as a group and then attempting to match the group:

...
'("$\(\\(.*\\)\)\\|\\1"
  .font-lock-preprocessor-face)
...

but I don't get any matches for group 1. Is this an allowable use of group references or do I have a syntax error?

Upvotes: 0

Views: 134

Answers (1)

"\\$\(.*\)" is a 6-character string: \$(.*). When interpreted as a regexp, it means “dollar, open parenthesis, any sequence of non-newline characters, close parenthesis”. You want the 8-character string \$\(.*\), which as a regexp means “dollar, start group, any sequence of non-newline characters, end group”. In Elisp syntax, a backslash in a string literal always quotes the next character if it isn't alphanumeric; unlike some other languages, Elisp doesn't know whether that string is supposed to be a regexp and there is no special dedoubling of backslashes.

Upvotes: 1

Related Questions