Question

I use QHighlighter class, and used regExp to highlight words in quotes:

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);
    }

}

It doesn't work. Work this:

"\".*\""

But it highlights unnecessary. What regular expression is correct?

Was it helpful?

Solution

Just higlight everything between quotes

QRegExp("\"([^\"]*)\"");

highlight single words (run in loop with offset to match words)

QRegExp("\"(\\w)*\"");

OTHER TIPS

How to match words in quotes:

('|")[^\1]*?\1

Example:

http://regex101.com/r/iF5aA1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top