![](/img/trans.png)
[英]Is there a way I can find out if a string in C# does not contain the numbers & characters 0-9 and A-Z?
[英]Does C# recognize numbers beginning with digits outside 0-9?
在C#中,如果詞匯標記以字母或下划線開頭,則識別為標識符或關鍵字,如果以數字開頭,則識別為數字。
在此上下文中,字母不限於[A-Za-z]
; 它可以是char.IsLetter
識別的任何Unicode字母。
為了識別數字文字, [0-9]
以外的任何字符是否都被類似地識別為數字?
答案是否定的,但不僅如此。
如果您查看語言規范 ,就會明白我的意思:
integer_literal
: decimal_integer_literal
| hexadecimal_integer_literal
;
decimal_integer_literal
: decimal_digit+ integer_type_suffix?
;
decimal_digit
: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
;
integer_type_suffix
: 'U' | 'u' | 'L' | 'l' | 'UL' | 'Ul' | 'uL' | 'ul' | 'LU' | 'Lu' | 'lU' | 'lu'
;
hexadecimal_integer_literal
: '0x' hex_digit+ integer_type_suffix?
| '0X' hex_digit+ integer_type_suffix?
;
hex_digit
: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
正如你所看到的, +
或-
不是整數文字規范的一部分,所以該規則的開始,它總是需要一個數字(或者0-9
的十進制整數,而0
,其次是x
為十六進制整數) 。
規則解析比僅檢查第一個字符以確定要遵循的規則要復雜得多。 閱讀有關ANTLR的前瞻性方法( LL(*) )將學到很多東西。 簡而言之,整個規則必須解析,否則將無法解析該規則。 它不只是基於規則的開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.