QTextDocument-Line-ending-and-Advanced-regular-expression-support

From Qt Wiki
Revision as of 17:20, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

QTextDocument Line Endings and Advanced RegExp

Warning: Experimental Info

I’ve picked from QtForum itself. It’s progressive article. I will update more when I will achieve something.

The find(regex) method of QTextDocument will not attempt to match across a “block” boundary (see answers in this post [developer.qt.nokia.com]). As a result, although the QRegExp doc says that \n is matched by .* and by \s, one can never get a match to \n in a QPlainTextEdit document. The \n is end of block and the search never spans a block. Thus it seems impossible for a pattern like

to find a match when the markup begins on one line and ends on another.

There is a way around this. The restriction is in QTextDocument; QRegExp does perform as documented when it is applied to a QString. So the following code can apply a general regex to a document. This is using PyQt4; the translation to C++ should be clear. Given is a QPlainTextEdit qpte whose cursor is the starting point for the search. Also given is a QRegExp qrxp that has been prepared with a search pattern such as “<b>.*</b>” and its minimal and case switches set.

There remains one restriction: QTextCursor.selectedText() returns a string in which the \n character has been replaced with Unicode paragraph separator. This character does match to \s and .* however to match a literal \n you have to change the pattern given to the regexp, replacing all \n with \x2029.