QRegExp和QSyntaxHighlighter的双引号文本 [英] QRegExp and double-quoted text for QSyntaxHighlighter

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

问题描述

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

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

测试模式

一个"或两个"或三个"

"one" or "two" or "three"

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

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:

注意:量词通常是贪婪的".它们始终匹配尽可能多的文本.例如,0 +会匹配找到的第一个零以及第一个零之后的所有连续零.应用于"20005",它与"20005"匹配.量词可以设为非贪婪,请参阅setMinimal().

Note: Quantifiers are normally "greedy". They always match as much text as they can. For example, 0+ matches the first zero it finds and all the consecutive zeros after the first zero. Applied to '20005', it matches'20005'. Quantifiers can be made non-greedy, see setMinimal().

如果使用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的双引号文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆