簡體   English   中英

正則表達式匹配C語言數字

[英]Regex expression to match C language numbers

我試圖匹配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時,正則表達式突出顯示。 我不想在前面加上字母或下划線時突出顯示數字,因為那時它將是一個標識符。

我怎么解決這個問題?

它匹配[因為這句話:

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

如果你想要的話,為什么你不匹配數字呢?

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

同樣@Jan Dvorak提到你可以使用單詞邊界\\b ,以確保你的匹配開始於單詞(或數字)的開頭。 請參見此處的示例: http//regex101.com/r/kC6dK3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM