This is fundamentally impossible with AvalonEdit's highlighting engine.
The engine is line-based, you cannot perform any multi-line matches. The only way to carry information from one line to the next is by opening a span -- the only state maintained by the highlighting engine is the stack of currently open spans.
The highlighting engine is designed this way to allow for incremental updates (which is critical for performance with large files). If you change text in a line, only that single line is updated. If this update leads to a change in the span stack at the end of the line, the following lines are updated as well (but only if they are in the visible portion of the text area - otherwise their update is delayed until the user scrolls down).
A possible solution is to implement your own IVisualLineTransformer
instead of using the syntax highlighting engine. Here is an example implementation that highlights all occurrences of the word 'AvalonEdit':
public class ColorizeAvalonEdit : DocumentColorizingTransformer
{
protected override void ColorizeLine(DocumentLine line)
{
int lineStartOffset = line.Offset;
string text = CurrentContext.Document.GetText(line);
int start = 0;
int index;
while ((index = text.IndexOf("AvalonEdit", start, StringComparison.Ordinal)) >= 0) {
base.ChangeLinePart(
lineStartOffset + index, // startOffset
lineStartOffset + index + 10, // endOffset
(VisualLineElement element) => {
// This lambda gets called once for every VisualLineElement
// between the specified offsets.
Typeface tf = element.TextRunProperties.Typeface;
// Replace the typeface with a modified version of
// the same typeface
element.TextRunProperties.SetTypeface(new Typeface(
tf.FontFamily,
FontStyles.Italic,
FontWeights.Bold,
tf.Stretch
));
});
start = index + 1; // search for next occurrence
}
}
}
// Usage:
textEditor.TextArea.TextView.LineTransformers.Add(new ColorizeAvalonEdit());