gpt4 book ai didi

c++ - QRegExp 和 QSyntaxHighlighter 的双引号文本

转载 作者:行者123 更新时间:2023-11-28 07:39:49 24 4
gpt4 key购买 nike

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

测试图案

"one" or "two" or "three"

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

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

最后一个模式在 regexpal.com 上成功,但在 QRegExp 类中失败。

最佳答案

编辑:如果您查看 Syntax Highlighter Example ,里面已经有这个了。

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 的荧光笔示例中复制大部分代码,您就应该设置好了。

QRegEx 中的贪婪与惰性匹配

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

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's regex engine来测试我的表情。

以下是您要查找的代码。它主要基于给定的示例 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();
}

希望对您有所帮助。

关于c++ - QRegExp 和 QSyntaxHighlighter 的双引号文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com