正规表达式突出引号QT5中的单词
-
21-12-2019 - |
题
我使用qhighlighter类,并使用regexp突出引号中的单词:
void Highlighter::highlightBlock(const QString &text)
{
QRegExp expr("\"(.*?)\"");
int index = expr.indexIn(text);
while(index >=0)
{
int length = expr.matchedLength();
setFormat(index, length, Qt::red);
index = expr.indexIn(text, index+length);
}
}
.
它不起作用。工作:
"\".*\""
.
但它突出了不必要的。正则表达式是正确的吗?
解决方案
只是在引号之间的所有内容
QRegExp("\"([^\"]*)\"");
.
突出显示单个单词(用偏移循环运行以匹配单词)
QRegExp("\"(\\w)*\"");
. 其他提示
不隶属于 StackOverflow