Reputation: 15
I am trying to write a custom syntax (via YAML), where I would like to indicate comment area rule. So for instance: /* */ - is a common from-to indication of a comment section.
Forged the following code:
((?=/*)[\s\S]*?(?<=*/))
It works well here: https://regex101.com/r/EQqn7b/2/
However, having tried it in Sublime, it wouldn't match text in the case with new lines between the beginning and ending of the comment section area:
Here is how it looks in Sublime
(Colored in gray - is the text which was successfully matched by the above regex)
So the desired effect is to make both cases shown in picture earlier - matched by regex in Sublime.
Could somebody point me out what am I doing wrong or missing out?
Thanks!
Note: flag like (?s) - is treated as unrecognized, rendering it unusable
Upvotes: 0
Views: 331
Reputation: 22821
You're bumping into this (excerpt from the syntax documentation, emphasis mine):
- match. The regex used to match against the text. YAML allows many strings to be written without quotes, which can help make the regex clearer, but it's important to understand when you need to quote the regex. If your regex includes the characters
#
,:
,-
,{
,[
or>
then you likely need to quote it. Regexes are only ever run against a single line of text at a time.
That is, your regex doesn't match across multiple lines because it's being fed the lines one at a time and not in the manner that you expect.
In order to support syntax constructs that span multiple lines, you need to use a second context
. An example of that might look something like this (taken from the default C syntax):
- match: /\*
scope: punctuation.definition.comment.c
push:
- meta_scope: comment.block.c
- match: \*/
scope: punctuation.definition.comment.c
pop: true
This says that when the text /*
matches, the parser should push a new anonymous context with it's own set of rules onto the parsing stack, which remains in effect until a match rule tells it to pop back to the context it was in to begin with.
The inner (anonymous) context has only a single match rules on */
with instructions to pop the context from the stack, which keeps any other syntax rules from matching until the end of the comment is seen.
This also shows scoping the /*
and */
portions of the match as comment punctuation individually, while the meta_scope
applies to the entire match (including the text that entered the context and the text that pops it off).
That makes the entire /* comment */
as a whole scope as comment.block.c
while also applying a specific context to the characters that start and stop it, since your color scheme may want to color them differently, or a plugin may want to be able to detect comment delimiters, etc.
Within that inner context
you can add as many extra match rules as you want. For example, you could include one that matches TODO
to give it an additional scope so that your color scheme can target them and make them stand out, and so on.
Upvotes: 4
Reputation: 6188
I believe your solution is not working because you are not using MULTILINE/DOTALL in your solution. You either pass those parameters to the method doing the evaluation, or simply add (?s)
at the beginning of your regular expression. See this post:
Sublime Text regex not detecting multiline tags
Upvotes: 0