Match fuzzy pattern, without previous words, but only for one occurence

136 Views Asked by At

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

There are 1 best solutions below

1
Jeffery To On

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>.