Regex for PascalCased words (aka camelCased with leading uppercase letter)

Solution 1:

([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.

Solution 2:

Lower camel case

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])?
  1. The first character is lower case.
  2. The following elements are either a single number or a upper case character followed by lower cases characters.
  3. The last character can be an upper case one.

Here is a snippet illustrating this regex. The following elements are valid.

xmlHttpRequest
newCustomerId
innerStopwatch
supportsIpv6OnIos
youTubeImporter
youtubeImporter
affine3D

Upper camel case

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