使用正则表达式表示标识符 [英] Representing identifiers using Regular Expression
问题描述
使用C编程语言识别标识符的常规定义由
The regular definition for recognizing identifiers in C programming language is given by
letter -> a|b|...z|A|B|...|Z|_
digit -> 0|1|...|9
identifier -> letter(letter|digit)*
此定义将生成格式为
标识符: [_ a-zA-Z] [_ a-zA-Z0-9] *
我现在的问题是如何将可生成的标识符的长度限制为不超过31个字符。需要对正则定义进行哪些更改,或者如何编写正则表达式以将其限制为不超过指定长度。谁能帮忙。谢谢。
My question now is how do you limit the length of the identifier that can be generated to not more than 31 characters. What changes need to be made in the regular definition or how to write a regular expression to limit it to not more than the specified length. Could anyone please help. Thanks.
推荐答案
您要查找的正则表达式为:
The regular expression you are looking for is:
[_a-zA-Z][_a-zA-Z0-9]{0,30}
它将匹配下划线或字母,后跟 X
下划线,字母或数字,其中 0< = X< = 30
It will match an underscore or letter following by X
underscores, letters or numbers, where 0 <= X <= 30
这篇关于使用正则表达式表示标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!