匹配 Camel 和 Pascal Case 的正则表达式
我即将为一种语言编写一个解析器,该语言应该对类型、变量等的命名有严格的语法规则。例如,所有类必须采用PascalCase,所有变量/参数名称和其他标识符必须采用camelCase。
例如,不允许使用 HTMLParser
,并且必须将其命名为 HtmlParser
。对于可以匹配 PascalCase 但其中没有两个大写字母的正则表达式有什么想法吗?
I'm about to write a parser for a language that's supposed to have strict syntactic rules about naming of types, variables and such. For example all classes must be PascalCase, and all variables/parameter names and other identifiers must be camelCase.
For example HTMLParser
is not allowed and must be named HtmlParser
. Any ideas for a regexp that can match something that is PascalCase, but does not have two capital letters in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
驼峰命名法:
帕斯卡命名法:
camelCase:
PascalCase:
^[AZ][az]*([AZ][az]*)
这应该适用于:
这种类型的模式。
^[A-Z][a-z]*([A-Z][a-z]*)
This should work for :
this types of patters.
但我不得不说你的命名选择很糟糕,HTMLParser 应该被允许和首选。
But I have to say your naming choice stinks, HTMLParser should be allowed and preferred.
我不相信列出的项目可以以数字开头(我想我在某个地方读过它,所以要持保留态度),所以最好的情况是像罗杰·佩特(Roger Pate)的一些小修改(在我看来)
应该是比如,寻找一个大写字母,然后至少一个小写字母或数字,或者更多,而且看起来它只处理一个大写字母,因为这似乎是必需的,但附加字母是可选的。
祝你好运
I don't believe the items listed can start with numbers (thought I read it somewhere so take it with a grain of salt) so the best case would be something like Roger Pate's with a few minor modifications (in my opinion)
Should be something like, Look for a Capital Letter, then at least one small case or number, or more, as well as it looks like it handles just a capital letter as that seems to be required, but the additional letters are optional.
Good luck
这允许 2 个连续的大写字符(这是普遍接受的,但不幸的是 PascalCase 不是规范)。
This allows 2 consecutive capital characters (which is generally accepted, but unluckily PascalCase is not a spec).
尽管原始帖子明确排除了两个连续的大写字母,但我想发布
PascalCase
的正则表达式,它将回答提出的许多评论:正则表达式为
^[AZ][a-z0-9]*(?:[AZ][a-z0-9]*) *(?:[AZ]?)$
当针对所有注释中提出的所有字符串进行测试时,以下内容匹配 PascalCase:
以下内容不匹配 PascalCase:
Although the original post specifically excluded two consecutive capital (uppercase) letters, I'd like to post the regex for
PascalCase
that will answer many comments raised:The regex is
^[A-Z][a-z0-9]*(?:[A-Z][a-z0-9]*)*(?:[A-Z]?)$
When tested against all strings raised in all comments, the following match as PascalCase:
The following do not match as PascalCase:
驼峰式 - 不允许使用数字
测试用例:https://regex101.com/library/4h7A1I
小 Camel Case - 允许的数字
测试用例: https://regex101.com/library/8nQras
Pascal Case -不允许使用数字
测试用例: https://regex101.com/library/sF2jRZ
Pascal Case - 允许使用数字
测试用例:https://regex101.com/library/csrkQw
有关驼峰案例和 pascal 的更多详细信息案例查看此repo。
Lower Camel Case - no digits allowed
Test Cases: https://regex101.com/library/4h7A1I
Lower Camel Case - digits allowed
Test Cases: https://regex101.com/library/8nQras
Pascal Case - no digits allowed
Test Cases: https://regex101.com/library/sF2jRZ
Pascal Case - digits allowed
Test Cases: https://regex101.com/library/csrkQw
For more details on camel case and pascal case check out this repo.