Results 1 to 6 of 6

Thread: Multiline match with QRegExp

  1. #1
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiline match with QRegExp

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

    Qt Code:
    1. Heading 1
    2. ===========
    3.  
    4. Heading 2
    5. -----------
    To copy to clipboard, switch view to plain text mode 

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


    EDIT:
    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)?
    Last edited by bender86; 3rd March 2011 at 12:07.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiline match with QRegExp

    Maybe you could count the block number and look at the next line via QSyntaxHighlighter::document()

  3. #3
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiline match with QRegExp

    Maybe I will try it, for now I just switched to the alternative straightforward syntax:
    Qt Code:
    1. # Heading 1 #
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiline match with QRegExp

    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.
    Last edited by pbek; 24th November 2016 at 10:16.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiline match with QRegExp

    The Kate editor 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.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    pbek (27th November 2016)

  7. #6
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiline match with QRegExp

    @anda_skoa, kate can't handle this kind of markdown headlines. Meanwhile I wrote my own highlighter https://github.com/pbek/qmarkdowntex...ighlighter.cpp. It will be used in version 16.11.15 of http://www.qownnotes.org/.

Similar Threads

  1. Multiline entry in a QMenu
    By tyrdal in forum Qt Programming
    Replies: 0
    Last Post: 28th September 2009, 11:54
  2. Replies: 1
    Last Post: 21st September 2009, 07:30
  3. Multiline QCombobox
    By tyrdal in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:37
  4. Multiline QPushButton
    By uestebanez in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2008, 21:25
  5. QRegExp match all excpet "_"
    By mattia in forum Newbie
    Replies: 6
    Last Post: 28th March 2008, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.