0
votes

I have lines that are of the type @variable a 2, from the beginning of the line. The keyword and its identifier @variable are known and fixed. The variables can have [_a-zA-Z] as the first character and then for the second character numbers, too, or combinations. They are separated by at least a space from variable, and at least a space from the value, which can be an expression, too, like (1+2)/2, or some other variable b, thus it starts with either a number, a letter, or a parenthesis. What follows after that in the line can be a lot of things, unimportant (among which also newline).

What I want to do is match the variable, but only if it's the first thing that comes after variable and is followed by a value or expression. This is what I have after the last attempt:

<context id="variable-noequal" style-ref="variable">
  <match extended="true">
    (((variable|variabel)\b\s+)?)
    (([_a-zA-Z][_a-zA-Z0-9]*))
    (?=((\s+)|(\s*\n)))
  </match>
</context>

I have tried various combinations (the hammer was involved, too), but I can either get no match, or match every match of a variable that might be following (like a 2 for goodness sake). Backwards looking always gives a lookbehind not fixed or some such error. I have also tried with prefix/suffix/keyword, where keyword was the fuzzy match you see above, but that didn't work, either. Searching the net revealed \K, which does nothing for me. I know that the match could be better, for example, as it is, a simple underscore could pass on as a variable, I think I can make that, but for now I just want to see it properly highlighted. I should probably mention I'm not versed in PCRE. Is there a fix for this?

Sorry, I forgot to add this is in GtkSourceView for gEdit, but that is based on PCRE, I think(?).

1
Try @variab(le|el)\s+[_a-zA-Z][_a-zA-Z0-9]*(?!\S), see regex101.com/r/j3otIZ/1. If it does not match what you expect, please explain what should be matched.Wiktor Stribiżew
@WiktorStribiżew Thanks for the comment, but I only want the variable, a, to be matched, not the whole thing until after it. If you try my code, it matches a, but also anything else that comes after it and looks like a variable, for example a 2 b 3, b is highlighted, too. It should also only work when there's @variable before, if you start deleting chars, it's highlighted, too. I want the matching to start right before a, stop right after a, and only after (nonhighlighted) @variable.a concerned citizen
So, if your regex engine is PCRE compatible, you may use @variab(?:le|el)\s+\K[_a-zA-Z][_a-zA-Z0-9]*(?!\S). If it is .NET compatible, use (?<=@variab(?:le|el)\s+)[_a-zA-Z][_a-zA-Z0-9]*(?!\S)Wiktor Stribiżew
@WiktorStribiżew If I test this on the regex site, it works, but if I use it inside the *.lang file for gEdit, it doesn't. As I mentioned, I already tried \K, but didn't work. Also, the 2nd method gives me that lookbehind assertion is not fixed length, again. I don't understand. Does GtkSourceView not support \K or ?<= (which I have to use as ?&lt;= to work)?a concerned citizen
You found a good reference. Use the @variab(?:le|el)\s+[_a-zA-Z][_a-zA-Z0-9]*\b in the parent context, and the [_a-zA-Z][_a-zA-Z0-9]* in the subcontext.Wiktor Stribiżew

1 Answers

0
votes

I originally posted this to your gtksourceview issue, but perhaps someone else may find this useful:

I believe something like this will do what you want:

<context id="variable-declaration">
  <start>@variable\%]</start>
  <end>(?=\S)</end>
  <include>
    <context id="variable-name" style-ref="variable" once-only="true">
      <match>[_a-zA-Z][_a-zA-Z0-9]*</match>
    </context>
  </include>
</context>

\%] is a custom word boundary that is like \b but can be redefined with <keyword-char-class>.