View Full Version : Multiline match with QRegExp

3rd March 2011, 11:33
I'm having some trouble using QRegExp to match a multiline expression. Actually I'm trying to write a Markdown syntax highlighter, and I'd like to recognize something like:

Heading 1

Heading 2

I tried ".+\\n=+" and similar, with no success.

I made some attempts with the RegExp example, and I found out I should be able to use "[^\\n][\\w\\s][^\\n]+\\n=+".
The problem is that QSyntaxHighlighter::highlightBlock() is called once per line, so I can't match anything which spans more than one line. Documentation suggests using a state based highlighter, but I don't think it would work since I know if a line is an heading only when I read the next line.

Any idea how to do that (except using alternative syntax for headings off course)?

3rd March 2011, 12:35
Maybe you could count the block number and look at the next line via QSyntaxHighlighter::document()

5th March 2011, 09:49
Maybe I will try it, for now I just switched to the alternative straightforward syntax:

# Heading 1 #

24th November 2016, 10:16
I've the same problem. I will attempt fetching the previous block with `currentBlock().previous()` or maybe the next block with `currentBlock().next()` to do checks...

Added after 13 minutes:

Has anyone here actually succeeded to write a markdown highlighter in C++ with QSyntaxHighlighter?
I've only found https://github.com/rupeshk/MarkdownHighlighter that is written in Python.

24th November 2016, 13:11
The Kate editor (https://kate-editor.org/) is a Qt based application that has markdown highlighting (among many others), but I think they are using their own engine.

The same highlight input files are also used by QtCreator, so it likely also has an implementation of that engine.


27th November 2016, 15:14
@anda_skoa, kate can't handle this kind of markdown headlines. Meanwhile I wrote my own highlighter https://github.com/pbek/qmarkdowntextedit/blob/develop/markdownhighlighter.cpp. It will be used in version 16.11.15 of http://www.qownnotes.org/.