本文介绍了QRegExp和QSyntaxHighlighter的双引号文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QRegExp模式用于捕获QSyntaxHighlighter的引用文本是什么?

What would the QRegExp pattern be for capturing quoted text for QSyntaxHighlighter?

测试模式

到目前为止,我已经尝试过:

So far I have tried:

QRegExp rx("\\0042.*\\0042");
QRegExp rx("(\\0042).*?\\1");

最后一个模式在regexpal.com上成功,但QRegExp类没有成功.

The last pattern succeeds on regexpal.com but not with the QRegExp class.

推荐答案

如果您查看语法突出显示示例,已经有一个.

If you check out the Syntax Highlighter Example, already has this one 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);

只需复制Qt荧光笔示例中的大多数代码,就应该设置好了.

Just copy most of the code from Qt's highlighter example and you should be set.

在Qt对RegEx的变体形式的描述中,它说:

In the description of Qt's variation on RegEx, it says:

如果使用setMinimal(true)来获得延迟匹配而不是贪婪匹配的效果,则可以将其关闭.其他正则表达式评估器使用*?+?之类的值来执行惰性匹配.有时我使用 gskinner的正则表达式引擎来测试我的表达式.

If you use setMinimal(true) to get the effect of lazy matching instead of greedy matching, you can pull it off. Other regex evaluators use something like *? or +? to perform a lazy match. Sometimes I use gskinner's regex engine to test my expressions.

下面是您要查找的代码.它很大程度上基于此处给出的示例.

Below is the code you are looking for. It is based heavily on the 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();
}

希望有帮助.

这篇关于QRegExp和QSyntaxHighlighter的双引号文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:46