Reputation: 1559
I am trying to match numbers in Qt for syntax highlighting, and I am trying the following regexes:
"[^a-fA-F_][0-9]+" // For numbers.
"[^a-fA-F_][0-9]+\\.[0-9]+" // For decimal numbers.
"[^a-fA-F_][0-9]+\\.[0-9]+e[0-9a-fA-F]+" // For scientific notation.
"[^a-fA-F_]0[xX][0-9a-fA-F]+" // For hexadecimal numbers.
But the text is matching, for example, [1024, and highlighting the [ too. I wanted to highlight only the 1024 part.
Another problem is that the regex highlights when I type aoe2 and when I type aoe25. I don't want to highlight the number when it is preceded by letters or underscores, because then it would be an identifier.
How can I solve that?
Upvotes: 1
Views: 4523
Reputation: 61540
Well it matches [
because of this statement:
[^a-fA-F_]
This will match anything that is not the letters A-F(any case) or an underscore
Why aren't you just matching the digits if that is what you want ?
For integers: \b\d+
For decimal numbers: \b\d+.\d+
For scientific: \b\d+e\d+
For hexadecimal: \b[\dA-Fa-F]+
Also as @Jan Dvorak mentions you can use word boundaries \b
, to make sure your matches begin at the beginning of a word (or number). See example here: http://regex101.com/r/kC6dK3
Upvotes: 4