Skip to content
ConsoleMonster
Class

RegexHighlighter

ConsoleMonster.Highlighting

Namespace
ConsoleMonster.Highlighting
Assembly
ConsoleMonster
Modifiers
sealed

A highlighter built from regular-expression rules, tried in order at each position (the first rule that matches wins). Use it to add a language without writing a lexer: Highlighters.Register(new RegexHighlighter("ini").Rule(@"^\s*;.*$", HighlightRole.Comment).Rule(...), "ini"). Matching has a time limit (default 250 ms per text); text after a timeout stays plain.

Inheritance

  • object
  • RegexHighlighter

Usage

Explore 2 API references to this type

This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.

Returned or exposed by 2

Constructors

public RegexHighlighter(string name, bool ignoreCase = false) #

Creates an empty highlighter.

Parameters

name string required
The language name.
ignoreCase bool = false optional
Whether the rules ignore case.

Methods

public RegexHighlighter Rule(string pattern, HighlightRole role) #

Adds a rule. Patterns run in multiline mode (^ and $ match at line breaks); avoid nested quantifiers.

Parameters

pattern string required
The pattern.
role ConsoleMonster.Highlighting.HighlightRole required
The role of what it matches.

Returns

RegexHighlighter This highlighter.
public IReadOnlyList<HighlightToken> Tokenize(string text) #

Tokenizes text.

Parameters

text string required
The text (any line endings).

Returns

IReadOnlyList<HighlightToken> The tokens, in order and not overlapping.
public RegexHighlighter Words(HighlightRole role, params string[] words) #

Adds a keyword rule: whole words from a list.

Parameters

role ConsoleMonster.Highlighting.HighlightRole required
The role.
words string[] required
The words.

Returns

RegexHighlighter This highlighter.

Properties

public string Name { get; } #

The language name, for example "json" (lower case).

public bool IgnoreCase { get; } #

Whether the rules ignore case.

public TimeSpan Timeout { get; set; } #

The time limit for one text (default 250 ms).