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

Tom Lehman picture Tom Lehman · Jul 14, 2009 · Viewed 40.9k times · Source

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).

Answer

Adam Crume picture Adam Crume · Jul 15, 2009
([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.