'vim syntax highlighting: region that starts with "#" and end with either "#" or end of line

The situation

I am in the process of writing a vim syntax file for a language that allows inline comments following this scheme:

This is code # and here is comment # but this is code again

The language also allows comment to start with a "#" and end with a newline, like so:

This is code # and until the end of the line, this is a comment

What I want

I would like both to be recognized by my syntax file as comments.

What I've tried

When I use:

syn region comment start="#" end="#"

, the result is that comments are not delimited by newlines. Now, I've done some research and tried it with the following regex match:

syn match comment "#.+?(?=#|$)"

And that seems to work properly, but doesn't do what I want. It does end the inline comment by the second "#", but then starts a new comment from that hashtag until the newline. regular expression tester result

How do I tell the vim syntax file to properly recognize the comments?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source