使用正则表达式表示标识符 [英] Representing identifiers using Regular Expression

查看:438
本文介绍了使用正则表达式表示标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆