QSyntaxHighlighter highlight part of match with QRegExp
i needed to match a string like the following:
- XXX
But both "1." and "X开发者_StackOverflowXX" is highlighted , and i'm currently using the following regex:
QRegExp ("^\s+(\d+\.)?\s+\b[A-Z]{2,}\b")
How can i highlight only XXX in this case ?
Many thanks !
Your regex should be something like:
QRegExp ("^\s+(\d+\.)?\s+(\b[A-Z]{2,}\b)")
so you can capture XXX in your regex. Then, you retrieve all matches using capturedTexts()
. The string you're after should be the last index since the first item is the entire string that matches, the second one would be the number and dot if found or the string XXX. If a number is present, the XXX will be in the third string.
Having that, you can then find the index of this substring inside the original one to setup your highlighting.
精彩评论