Domanda

I am trying to come up with a regex for both Upper and lower Camel case.

Here is what I tried

(([A-Z][a-z0-9]*){2,}|([a-z][A-Z0-9]*){2,})

Trying to match Upper camel case with this - ([A-Z][a-z0-9]){2,} but it is matching other combinations as well. Similar is the case with the second part - ([a-z][A-Z0-9]){2,})

È stato utile?

Soluzione

This would match upper and lower camel case phrases containing at least one upper case in the word.

Upper Camel Case

[A-Z][a-z0-9]*[A-Z0-9][a-z0-9]+[A-Za-z0-9]*

example:HelloWorld, AQuickBrownFox

Lower Camel Case

[a-z]+[A-Z0-9][a-z0-9]+[A-Za-z0-9]*

example: helloWorld, aQuickBrownFox

Altri suggerimenti

For lowerCamelCase you need:

  1. A lowerCaseLetter
  2. at least one (lowerCaseLetter or UpperCaseLetter or numb3r)

So an approriate regex would be

[a-z][a-zA-Z0-9]+

Similarly for UpperCamelCase, you'll have [A-Z][a-zA-Z0-9]+, and if you group those, you get

[a-zA-Z][a-zA-Z0-9]+

Edit: If you strictly require that for a word to be a camel case word, it heeds to have a "hump", where a hump is an uppercase letter or a number, you need:

  1. An upper or a lower case letter, followed by
  2. Other lower case letters (maybe none), followed by
  3. A hump, followed by
  4. Other lower case letters (maybe none),
  5. Maybe followed by another hump(s)

Then your regex is:

[a-zA-Z][a-z]*([A-Z0-9]+[a-z]*)+

Regex fiddle

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top