匹配 Camel 和 Pascal Case 的正则表达式

发布于 2024-08-18 04:37:02 字数 233 浏览 2 评论 0原文

我即将为一种语言编写一个解析器,该语言应该对类型、变量等的命名有严格的语法规则。例如,所有类必须采用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅笑依然 2024-08-25 04:37:02

驼峰命名法:

^[a-z]+(?:[A-Z][a-z]+)*$

帕斯卡命名法:

^[A-Z][a-z]+(?:[A-Z][a-z]+)*$

camelCase:

^[a-z]+(?:[A-Z][a-z]+)*$

PascalCase:

^[A-Z][a-z]+(?:[A-Z][a-z]+)*$
自我难过 2024-08-25 04:37:02

^[AZ][az]*([AZ][az]*)

这应该适用于:

  1. MadeEasy
  2. WonderFul
  3. AndMe

这种类型的模式。

^[A-Z][a-z]*([A-Z][a-z]*)

This should work for :

  1. MadeEasy
  2. WonderFul
  3. AndMe

this types of patters.

/([A-Z][a-z]+)*[A-Z][a-z]*/

但我不得不说你的命名选择很糟糕,HTMLParser 应该被允许和首选。

/([A-Z][a-z]+)*[A-Z][a-z]*/

But I have to say your naming choice stinks, HTMLParser should be allowed and preferred.

追风人 2024-08-25 04:37:02

我不相信列出的项目可以以数字开头(我想我在某个地方读过它,所以要持保留态度),所以最好的情况是像罗杰·佩特(Roger Pate)的一些小修改(在我看来)

/^([A-Z][a-z0-9]+)*[A-Z][a-z0-9]*$/

应该是比如,寻找一个大写字母,然后至少一个小写字母或数字,或者更多,而且看起来它只处理一个大写字母,因为这似乎是必需的,但附加字母是可选的。

祝你好运

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)

/^([A-Z][a-z0-9]+)*[A-Z][a-z0-9]*$/

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

岁月蹉跎了容颜 2024-08-25 04:37:02
^[A-Z]{1,2}([a-z]+[A-Z]{0,2})*$

这允许 2 个连续的大写字符(这是普遍接受的,但不幸的是 PascalCase 不是规范)。

^[A-Z]{1,2}([a-z]+[A-Z]{0,2})*$

This allows 2 consecutive capital characters (which is generally accepted, but unluckily PascalCase is not a spec).

无可置疑 2024-08-25 04:37:02

尽管原始帖子明确排除了两个连续的大写字母,但我想发布 PascalCase 的正则表达式,它将回答提出的许多评论:

  • 允许两个连续的大写字母
  • 允许数字(但不作为字符串中的前导字符)
  • 允许以大写字母或数字结尾的字符串

正则表达式为 ^[AZ][a-z0-9]*(?:[AZ][a-z0-9]*) *(?:[AZ]?)$

当针对所有注释中提出的所有字符串进行测试时,以下内容匹配 PascalCase:

PascalCase
Pascal2Case
PascalCaseA
Pascal2CaseA
ModeA
Mode2A
Mode2A2
Mode2A2A
CreateAMode
CreateBMode
MadeEasy
WonderFul
AndMe
Context
HTMLParser
HtmlParser
H
AaA
HELLO

以下内容匹配 PascalCase:

camelCase
2PascalCase

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:

  • Allowing two consecutive capital letters
  • Allowing digits (but not as the leading character in the string)
  • Allowing a string ending with a capital letter or a digit

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:

PascalCase
Pascal2Case
PascalCaseA
Pascal2CaseA
ModeA
Mode2A
Mode2A2
Mode2A2A
CreateAMode
CreateBMode
MadeEasy
WonderFul
AndMe
Context
HTMLParser
HtmlParser
H
AaA
HELLO

The following do not match as PascalCase:

camelCase
2PascalCase
愿与i 2024-08-25 04:37:02

驼峰式 - 不允许使用数字


    ^[a-z][a-z]*(([A-Z][a-z]+)*[A-Z]?|([a-z]+[A-Z])*|[A-Z])$
    

测试用例:https://regex101.com/library/4h7A1I

小 Camel Case - 允许的数字


    ^[a-z][a-z0-9]*(([A-Z][a-z0-9]+)*[A-Z]?|([a-z0-9]+[A-Z])*|[A-Z])$

测试用例: https://regex101.com/library/8nQras

Pascal Case -不允许使用数字


    ^[A-Z](([a-z]+[A-Z]?)*)$

测试用例: https://regex101.com/library/sF2jRZ

Pascal Case - 允许使用数字


    ^[A-Z](([a-z0-9]+[A-Z]?)*)$

测试用例:https://regex101.com/library/csrkQw

有关驼峰案例和 pascal 的更多详细信息案例查看此repo

Lower Camel Case - no digits allowed


    ^[a-z][a-z]*(([A-Z][a-z]+)*[A-Z]?|([a-z]+[A-Z])*|[A-Z])$
    

Test Cases: https://regex101.com/library/4h7A1I

Lower Camel Case - digits allowed


    ^[a-z][a-z0-9]*(([A-Z][a-z0-9]+)*[A-Z]?|([a-z0-9]+[A-Z])*|[A-Z])$

Test Cases: https://regex101.com/library/8nQras

Pascal Case - no digits allowed


    ^[A-Z](([a-z]+[A-Z]?)*)$

Test Cases: https://regex101.com/library/sF2jRZ

Pascal Case - digits allowed


    ^[A-Z](([a-z0-9]+[A-Z]?)*)$

Test Cases: https://regex101.com/library/csrkQw

For more details on camel case and pascal case check out this repo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文