How do I find all PascalCased words in a document with a regular expression?
If you don't know the word Pascal cased, I'm only concerned with leading Upper camel case (i.e., camel cased words in which the first letter is capitalized).
For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
What is CamelCase? CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces. It is commonly used in web URLs, programming and computer naming conventions. It is named after camels because the capital letters resemble the humps on a camel's back.
([A-Z][a-z0-9]+)+
Assuming English. Use appropriate character classes if you want it internationalizable. This will match words such as "This". If you want to only match words with at least two capitals, just use
([A-Z][a-z0-9]+){2,}
UPDATE: As I mentioned in a comment, a better version is:
[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*
It matches strings that start with an uppercase letter, contain only letters and numbers, and contain at least one lowercase letter and at least one other uppercase letter.
this regex includes number and implements strict lower camel case as defined by the Google Java Style Guide regex validation.
[a-z]+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?
Here is a snippet illustrating this regex. The following elements are valid.
xmlHttpRequest newCustomerId innerStopwatch supportsIpv6OnIos youTubeImporter youtubeImporter affine3D
Same principle as the one used for lower camel case with always a starting upper case character.
([A-Z][a-z0-9]+)((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?
Here is a snippet illustrating this regex. The following elements are valid.
XmlHttpRequest NewCustomerId InnerStopwatch SupportsIpv6OnIos YouTubeImporter YoutubeImporter Affine3D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With