c++ - QRegExp and double-quoted text for QSyntaxHighlighter -
what qregexp pattern capturing quoted text qsyntaxhighlighter?
test pattern
"one" or "two" or "three"
so far have tried:
qregexp rx("\\0042.*\\0042"); qregexp rx("(\\0042).*?\\1");
the last pattern succeeds on regexpal.com not qregexp class.
edit: if check out syntax highlighter example, has 1 in there.
http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter-highlighter-cpp.html
quotationformat.setforeground(qt::darkgreen); rule.pattern = qregexp("\".*\""); rule.format = quotationformat; highlightingrules.append(rule);
just copy of code qt's highlighter example , should set.
greedy v. lazy match in qregex
in description of qt's variation on regex, says:
note: quantifiers "greedy". match text can. example, 0+ matches first 0 finds , consecutive zeros after first zero. applied '20005', matches'20005'. quantifiers can made non-greedy, see setminimal().
if use setminimal(true)
effect of lazy matching instead of greedy matching, can pull off. other regex evaluators use *?
or +?
perform lazy match. use gskinner's regex engine test expressions.
below code looking for. based heavily on example given here.
#include <qcoreapplication> #include <qregexp> #include <qdebug> #include <qstringlist> int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); qstring str = "\"one\" or \"two\" or \"three\""; qregexp rx("\".*\""); rx.setminimal(true); int count = 0; int pos = 0; while ((pos = rx.indexin(str, pos)) != -1) { ++count; pos += rx.matchedlength(); qdebug() << rx.cap(); } return a.exec(); }
hope helps.
Comments
Post a Comment