问题
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?
回答1:
camelCase:
^[a-z]+(?:[A-Z][a-z]+)*$
PascalCase:
^[A-Z][a-z]+(?:[A-Z][a-z]+)*$
回答2:
/([A-Z][a-z]+)*[A-Z][a-z]*/
But I have to say your naming choice stinks, HTMLParser should be allowed and preferred.
回答3:
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
回答4:
^[A-Z][a-z]*([A-Z][a-z]*)
This should work for :
- MadeEasy
- WonderFul
- AndMe
this types of patters.
来源:https://stackoverflow.com/questions/2103596/regex-that-matches-camel-and-pascal-case