Windows 7 SP1
MSVS 2010
Qt 4.8.4
I’m experimenting with the Qt Syntax Highlighter Example.
I have an application that needs to highlight words that start with a hyphen. So I modify the regular expression from this code fragment:
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
which highlights words that start with Q. I change it to:
rule.pattern = QRegExp("\\b-[A-Za-z]+\\b");
and nothing happens.
I try
rule.pattern = QRegExp("\\b\\-[A-Za-z]+\\b");
Nothing.
Out of curiosity, I try
rule.pattern = QRegExp("\\b[-A-Za-z]+\\b");
If I start typing a hyphen, the hyphen is unhighlighted and every other alpha is highlighted. According to How to match hyphens with Regular Expression? this should be kosher.
Question: How do I write the regular expression to highlight words starting with a hyphen?
The problem is that a hyphen
-is considered not being part of a word. This means, that the word boundary assertion\bwill match between the hyphen and the actual word. In other words: There is no word starting with a hyphen.To solve this issue, place the hyphen before
\b, meaning you want to match “a hyphen, followed by a word consisting of letters”. You can even remove the first\b, because[a-zA-Z]+is a word anyway: