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
(?:[A-Z])(?:[a-z])+(?:[A-Z])(?:[a-z])+
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
McDowell
O'Connor
T.Kasting
Edit2
I have modified my regex based on the suggestion in the comments
(?:[A-Z])(?:\S?)+(?:[A-Z])(?:[a-z])+
You could check if a string has both upper and lowercase.
def is_camel_case(s):
return s != s.lower() and s != s.upper() and "_" not in s
tests = [
"camel",
"camelCase",
"CamelCase",
"CAMELCASE",
"camelcase",
"Camelcase",
"Case",
"camel_case",
]
for test in tests:
print(test, is_camel_case(test))
Output:
camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase True
Case True
camel_case False
You probably want something more like:
(?:[A-Z][a-z]*)+
Altho that would allow all caps. You could avoid that with:
(?:[A-Z][a-z]+)+
Anchor the expression with ^
and $
or \\z
if required.
Convert your string to camel case using a library like inflection
. If it doesn't change, it must've already been camel case.
from inflection import camelize
def is_camel_case(s):
# return True for both 'CamelCase' and 'camelCase'
return camelize(s) == s or camelize(s, False) == s
I think you might get away with just checking that the string has a capital with a lower case letter before it if(line =~ m/[az][AZ]/). Just checking lower and upper fails on the given examples. ~Ben
sub_string= 'hiSantyWhereAreYou' `Change the string here and try`
index=[x for x,y in enumerate(list(sub_string)) if(y.isupper())] `Finding the index
of caps`
camel_=[]
temp=0
for m in index:
camel_.append(sub_string[temp:m])
temp=m
if(len(index)>0):
camel_.append(sub_string[index[-1]::])
print('The individual camel case words are', camel_) `Output is in list`
else:
camel_.append(sub_string)
print('The given string is not camel Case')
if you do not want strings starting with upper-case eg Case
and Camelcase
to pass True
. edit @william's answer:
def is_camel_case(s):
if s != s.lower() and s != s.upper() and "_" not in s and sum(i.isupper() for i in s[1:-1]) == 1:
return True
return False
tests = [
"camel",
"camelCase",
"CamelCase",
"CAMELCASE",
"camelcase",
"Camelcase",
"Case",
"camel_case",
]
for test in tests:
print(test, is_camel_case(test))
the results:
camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase False
Case False
camel_case False
这个正则表达式解决方案适用于我的用例([AZ][az\\S]+)([AZ][az]+)
is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter
def iscamelcase(string):
non_alpha = [i for i in string if not i.isalpha()]
substrings= string.translate({ord(i): ' ' for i in non_alpha}).split(' ')
for string in substrings:
if not all(char.isupper() for char in string):
for idx,i in enumerate(string):
if i.isupper() and idx > 0:
return True
return False
non_alpha
substrings
Output
camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase False
Case False
camel_case False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.