Question

i´m trying to realize a Syntax-Highlighting for a Heredoc-similar Syntax. therefore i have to capture the starting-name. but i failed (trying it in a Rule and a Span). This is my Regex:

(?:([A-Z]{3,}))(.|\n)*?\1

The regex works in a regex-tester.

But if i put it in the XSHD-Definition i get the error:

Error at line 128:
"(?:([A-Z]{3,}))(.|\n)*?\1" wird analysiert - Verweis auf die nicht definierte Gruppenzahl 1.

it says that the groupnumber 1 isn´t defined.

does somebody know the reason? and how to realize the matching?

thank you

Was it helpful?

Solution

AvalonEdit is using RegexOptions.ExplicitCapture, so you'll have to use named capture groups.

However, what you are trying to do is not possible in AvalonEdit. Rules cannot be multi-line, and the span end cannot use backrefences to the span start.

This is because the highlighting engine highlights each line individually, and will only re-highlighting changed lines when the user is editing text. To support HEREDOC constructs, AvalonEdit would need to store the results of named capture groups as part of the span stacks, and would need to support some special syntax so that the span end regex could use backreferences to those stored results.

OTHER TIPS

"(?:([A-Z]{3,}))(.|\n)*?\1"

Hmm, the first parenthetical (?: ) is a non-capturing group, for when you want to group (say for the | operator), but you don't care to 'remember' it. The second parenthetical, what you want to be group #1, is inside that one: ([A-Z]{3,}). It would seem to be logical that by saying "don't remember what is inside this", it won't.

Anyway, what is the point of the doubled up groups, with the outer being non capturing? (also no need for comma when you don't specify the second part of the quantifier sequence) Can't you just say:

"([A-Z]{3})[.\n]*?\1"   //now \1 is defined?

as far as your THIRD group, (.|\n)*?, in which you've used grouping+alternation+quantifiers all at once, well I have no idea what \3 might be if you tried to access it in the original expression: [.\n]*? should be fine.

*?

repeats the back-reference itseft.

how about

(?:([A-Z]{3,}))[.\n]*?\1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top