0

The goal is to highlight text inside [...] with a different color than text inside ![...]!

Also everything should be nestable such that [...] could contain ![...]! inside of it or vice versa and the colors should be different as desired.

I tried the following regex to try and match them differently but unfortunately wasn't able to succeed with it.

syn region  squareBracketRegex_A start="^\[\|[^!]\[" skip="\[[^\]]*\]" end="\]" keepend oneline contains=squareBracketRegex_B
syn region  squareBracketRegex_B start="!\[" skip="!\[[^\]]!*\]!" end="\]!" keepend oneline contains=squareBracketRegex_A
hi def link squareBracketRegex_A ColorGreen
hi def link squareBracketRegex_B ColorRed

Remark: I would like that the brackets themselves should take highlighting of whatever item they're contained in.

New contributor
yousefx0 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

1

Despite the fact that the highlighting groups ColorGreen and ColorRed are not defined I can't reproduce your problem.

The following syntax seems to work fine for me:

syn region squareBracketRegex_A start="\(!\@<!\[\)\@<=" skip="\[[^\]]*\]" end="\ze\]" keepend oneline contains=squareBracketRegex_B
syn region squareBracketRegex_B start="\(!\[\)\@<=" skip="!\[[^\]!]*\]!" end="\ze\]!" keepend oneline contains=squareBracketRegex_A
hi ColorGreen guifg=#008000
hi ColorRed guifg=#FF0000
hi def link squareBracketRegex_A ColorGreen
hi def link squareBracketRegex_B ColorRed

Remark: I have replaced ^\[\|[^!]\[ by !\@<!\[ but your version works also fine on my end.

Comments:

  • !\@<! match only if the preceding token is not ! (more information with :help /\@<!)
  • Hence !\@<!\[ match [ unless there is a ! in front of it
  • \(...\)\@<= match with zero width if ... match right before. This to avoid that the opening token: ![ is included in the region (more information with :help /\@<=)
  • \ze]! match with zero width right before ]!. This to avoid that the closing ]! is inclucded in the region (more information with :help /\ze)
7
  • 2
    Actually I want to highlight the text inside the brackets only and not the brackets too. The brackets themselves should take highlighting of whatever item they're contained in.
    – yousefx0
    Commented 15 hours ago
  • Thanks for the feedback! Maybe could you includes this demand in the original question.
    – Vivian De Smedt
    Commented 14 hours ago
  • I have modified the solution to take your feedback into account. It seems to work fine on my end. Let us know what part is not working at your end if any. An example of text and the corresponding screenshot helps to understand the problem :-)
    – Vivian De Smedt
    Commented 14 hours ago
  • 2
    Thanks a lot! Everything works as expected now. Do you mind explaining your solution? I would really like to understand the matching patterns you came up with.
    – yousefx0
    Commented 13 hours ago
  • 1
    Thanks a lot! Everything is clear now.
    – yousefx0
    Commented 12 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.