I'm trying to use the following regex to match 'Capitalized' words:


Which matches 'Main' and 'Canvas' in:

public class Main extends Canvas

Or another example, matches all occurrences of 'Bitmap':

public Bitmap b = new Bitmap(); And InputHandler in:

addKeyListener(new InputHandler())

This works fairly nice when used in search mode but not completely when using syntax match in a syntax file, where the command would be:

syntax match Capitalized "\l\@<!\<\u\w\+" This does not match anything that is inside parenthesis. Take one example from above:

addKeyListener(new InputHandler()) InputHandler is matched in search mode, but not from a syntax match command.

Am I missing something?

  • Copied your example lines, and used your "syntax match..." (also copy-past). Added "hi def link Capitalized Error" to get some color. It works for me. Im using (g)vim 8.1.565. – Ralf Dec 10 '18 at 4:57
  • 2
    I think the answer you seek is not about the regex matching, but about how syntax specifications work. I would guess that the problem is related to the use of contains and contained, in that the parantheses are matched by a syntax region command, and everything that can be matched inside the parantheses must be defined with properly defined containedin=.... – Karl Yngve Lervåg Dec 10 '18 at 8:01

Thank you so very much @KarlYngveLervåg for pointing out :h :syn-containedin, it leaded me towards the solution:

syntax match Capitalized "\l\@<!\<\u\w\+" containedin=parenGroup

Where parenGroup in my case is a 'syntax region' that also contains the top cluster. More specifically the exact solution was (for java.vim):

syntax match Capitalized "\l\@<!\<\u\w\+" containedin=javaParenT

No need to define a new region or crazy parenthesis matching.

Note: If your parent region definition doesn't include the top cluster this might not work outside parenthesis!

Summarizing, vim syntax definitions has some gotchas!

Again , thanks!

Update! My previous solution would not match KeyEvent in:

if (input.keys[KeyEvent.VK_A])

Now changing the syntax specification to (java only!):

syntax match capitalInParen transparent matchgroup=javaParen start="(" end="\]\w\+)" contains=@javaTop
syntax match Capitalized "\l\@<!\<\u\w\+" containedin=capitalInParent

This is not perfect but still much better than the last one.

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.