Reusing parts of regexps in syntax files

0 views
Skip to first unread message

Szabolcs

unread,
Jan 30, 2008, 8:45:30 AM1/30/08
to vim_use
When writing syntax files, is it possible to store pieces of regular
expressions in a variable, and reuse them in several match patterns?

This would have been useful for me when trying to get right an unusual
kind of floating point format, which may look like this: 12.3`4.5*^6
(This just means 12.3 * 10^6 with 4.5 digits of precision.) I think
I got it working correctly for the limited case decimal numbers, but
it looks terribly unreadable and I made several mistakes before I got
it right. I'm also interested in how this could be improved:

syntax match someInteger display "\<\d\+\(\*\^\d\+\)\="
syntax match someFloat display "\<\d\+\.\d*\(`\{1,2}\(\(\d\+\(\.\d*\)
\=\)\|\(\.\d+\)\)\=\)\=\(\*\^\d\+\)\="
syntax match someFloat display "\.\d\+\(`\{1,2}\(\(\d\+\(\.\d*\)\=\)\|
\(\.\d+\)\)\=\)\=\(\*\^\d\+\)\="
syntax match someFloat display "\<\d\+`\{1,2}\(\(\d\+\(\.\d*\)\=\)\|\
(\.\d+\)\)\=\(\*\^\d\+\)\="


Note how some of the pieces are repeated:

This is the part following the ` (or ``) sign, repeated 3x: \(\(\d\+\
(\.\d*\)\=\)\|\(\.\d+\)\)\=
And this is the optional exponent, repeated 4x: \(\*\^\d\+\)\=

It would be nice to simplify this before I try to extend it to bases
other than 10, e.g. in base 18 this should be working: 18^^h.
2g``5.7*^2 (it is 5560.`10.9 in decimal notation).

Charles E Campbell Jr

unread,
Jan 30, 2008, 9:57:44 AM1/30/08
to vim...@googlegroups.com
Szabolcs wrote:

You could use execute (:help execute).

let s:ifmt_re= '\<\d\+\(\*\^\d\+\)\='
exe "syn match someInteger display ".s:ifmt_re

would duplicate your first "someInteger" syntax, for example. I used s:
variables to prevent namespace pollution, btw.

Regards,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages