正则表达式匹配骆驼和帕斯卡案例 [英] Regex that matches Camel and Pascal Case

查看:298
本文介绍了正则表达式匹配骆驼和帕斯卡案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将为一种语言编写一个解析器,该语言应该具有关于类型,变量等的命名的严格语法规则.例如,所有类必须必须是PascalCase,所有变量/参数名称和其他标识符必须必须是camelCase.

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.

例如,不允许HTMLParser,并且必须将其命名为HtmlParser.有什么正则表达式的想法可以匹配PascalCase,但其中没有两个大写字母吗?

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?

推荐答案

camelCase:

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

PascalCase:

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

这篇关于正则表达式匹配骆驼和帕斯卡案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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