I would like to check if a string is a camel case or not (boolean). I am inclined to use a regex but any other elegant solution would work. I wrote a simple regex
JavaScript
x
2
1
(?:[A-Z])(?:[a-z])+(?:[A-Z])(?:[a-z])+
2
Would this be correct? Or am I missing something?
Edit
I would like to capture names in a collection of text documents of the format
JavaScript
1
4
1
McDowell
2
O'Connor
3
T.Kasting
4
Edit2
I have modified my regex based on the suggestion in the comments
JavaScript
1
2
1
(?:[A-Z])(?:S?)+(?:[A-Z])(?:[a-z])+
2
Advertisement
Answer
You could check if a string has both upper and lowercase.
JavaScript
1
18
18
1
def is_camel_case(s):
2
return s != s.lower() and s != s.upper() and "_" not in s
3
4
5
tests = [
6
"camel",
7
"camelCase",
8
"CamelCase",
9
"CAMELCASE",
10
"camelcase",
11
"Camelcase",
12
"Case",
13
"camel_case",
14
]
15
16
for test in tests:
17
print(test, is_camel_case(test))
18
Output:
JavaScript
1
9
1
camel False
2
camelCase True
3
CamelCase True
4
CAMELCASE False
5
camelcase False
6
Camelcase True
7
Case True
8
camel_case False
9