[英]Regex expression to match C language numbers
I am trying to match numbers in Qt for syntax highlighting, and I am trying the following regexes: 我试图匹配Qt中的数字语法高亮,我正在尝试以下正则表达式:
"[^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. 但文本是匹配的,例如,[1024,并突出显示[也是。 I wanted to highlight only the 1024 part.
我想只突出1024部分。
Another problem is that the regex highlights when I type aoe2 and when I type aoe25. 另一个问题是,当我输入aoe2和输入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? 我怎么解决这个问题?
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). 同样@Jan Dvorak提到你可以使用单词边界
\\b
,以确保你的匹配开始于单词(或数字)的开头。 See example here: http://regex101.com/r/kC6dK3 请参见此处的示例: http : //regex101.com/r/kC6dK3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.