QRegExp for String in quotes
-
Hi,
I am looking for the right QRegExp to detect a string within quotes. The expression should capture singe and double quotes and recognise escaped quotes. In PHP this would be the right one but it does not work in a QRegExp.
([\"'])(?:[^\1\\]|\\.)*?\1Thanks
-
Hi,
Are you using Qt 4 or 5, if Qt 5 then change for QRegularExpression.
You can also use QRegularExpression tool to test and validate your regular expressions.
-
Do you mean the syntax highlighter example ?
You also have the regular expression tool for QRegExp.
-
Hi all,
thanks for the help so far. Yes, this is the example I am working with. The issue is in the header file
private: struct HighlightingRule { QRegExp pattern; // <- here QTextCharFormat format; };When I change this line to QRegularExpression pattern; I get an error:
\components\highlighter.h:80: Fehler: field 'pattern' has incomplete type 'QRegularExpression' QRegularExpression pattern;
I have too little knowledge about C++ or Qt to understand that.
-
Wow that is the solution. I did not think of that because Qt Creator picked it up in auto complete so I guessed it was included...
So what I did in case someone is facing the same issue:
In the syntax highlighter example I exchanged all QRegExp with QRegularExpression (except on the multiline thing) and included QRegularExpression in the header file. In the cpp file I changed the block function like this:void Highlighter::highlightBlock(const QString &text) { foreach (const HighlightingRule &rule, highlightingRules) { QRegularExpression exp(rule.pattern); QRegularExpressionMatchIterator matches = exp.globalMatch(text); while (matches.hasNext()) { QRegularExpressionMatch match = matches.next(); setFormat(match.capturedStart(), match.capturedLength(), rule.format); } } [...]This works but throws a lot of warning:
QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object
So I guess the code can be optimized...Thanks again for the help.
-
I have an update to the example almost ready, I'll submit it later when done.
-
const QRegularExpression matchQuoted(R"**((?<!\\)([\"'])(.+?)(?<!\\)\1)**");recognise escaped quotes
Here I suppose that to escape a quote you use the C way (
\") rather than the VBA way ("") -
@SGaist said in QRegExp for String in quotes:
I have an update to the example almost ready, I'll submit it later when done.
I am interested to see that because I was not able to do a multiline String highlight with the expression suggested by VRonin.
I do not really understand how this works with the blocks and everything. So probably the text variable is not containing the right section for the expression to work.