匹配C语言数字的正则表达式

Regex expression to match C language numbers

本文关键字:正则表达式 数字 语言 匹配      更新时间:2023-10-16

我试图匹配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.

但是文本匹配,例如,[1024,并突出显示[。我想只突出显示1024的部分。

另一个问题是,当我键入aoe2和aoe25时,regex突出显示。当数字前面有字母或下划线时,我不想突出显示它,因为那样它将是一个标识符。

我怎么解决这个问题?

它匹配[,因为这个语句:

[^a-fA-F_]
This will match anything that is not the letters A-F(any case) or an underscore

如果你想要的话,为什么不直接匹配数字呢?

For integers:         bd+
For decimal numbers:  bd+.d+
For scientific:       bd+ed+
For hexadecimal:      b[dA-Fa-F]+

也正如@Jan Dvorak提到的,你可以使用单词边界b,以确保你的匹配从单词(或数字)的开头开始。示例如下:http://regex101.com/r/kC6dK3