Python中检查驼峰命名法

10

我希望检查一个字符串是否为驼峰命名法(布尔值)。我倾向于使用正则表达式,但任何其他优雅的解决方案也可以。我编写了一个简单的正则表达式

(?:[A-Z])(?:[a-z])+(?:[A-Z])(?:[a-z])+

这个对吗?还是我漏掉了什么?

编辑

我想在一系列文本文档中捕获格式为的名称集合

McDowell
O'Connor
T.Kasting

编辑2

根据评论中的建议,我已经修改了我的正则表达式。

(?:[A-Z])(?:\S?)+(?:[A-Z])(?:[a-z])+

2
这是一个比较难以通过程序来确定的事情。camel 是驼峰式命名吗?那 _camelCamel_CamelCONSTCAMELHTML 或者 var_camelCase 呢?除非你提前知道格式,否则很难定义。 - Silas Ray
但是像比尔·威廉姆斯这样的名字会通过您的过滤器...无论如何,那么任何以大写字母开头并包含第二个大写字母的标记呢?第二个大写字母不与第一个相邻。 - Silas Ray
@sr2222 是的,你说得对。正则表达式正确吗? - Dexter
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/10141/discussion-between-mcenley-and-sr2222 - Dexter
1
你能提供一些你希望失败的输入示例吗? - William Bettridge-Radford
显示剩余10条评论
8个回答

19

您可以检查字符串是否包含大写和小写字母。

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

输出:

camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase True
Case True
camel_case False

5
这肯定行不通吧?CAMELcase 不是有效的,会返回 true。 - Strobe_
2
这取决于您是否想允许缩写词,例如DecodeRGBDecodeRgb - William Bettridge-Radford
这对于 _camelCase 返回 false。 - user3064538
2
为什么这是被接受的答案?这不正确。 - benmaq
也不能很好地处理数字或标点符号。 - Tobias P. G.
还应该检查.isalnum()吗?并且还要检查字符串不以数字开头吗? - Andrey Pokhilko

3
您可能更希望使用以下内容:
(?:[A-Z][a-z]*)+

虽然这样可以使用全部大写字母,但您可以通过以下方式避免:

(?:[A-Z][a-z]+)+

将表达式使用^$\z 进行锚定,如果需要的话。

2

如果您不希望以大写字母开头的字符串,例如CaseCamelcase通过True。请编辑@william的答案:

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

结果如下:

camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase False
Case False
camel_case False

1
"CAMELCASE"、"Camelcase" 和 "Case" 都是有效的 CamelCase 字符串。 - MattS
1
"CAMELCASE"、"Camelcase"和"Case"都是有效的驼峰命名字符串。 - MattS
1
@MattS 问题是这是否有用。在许多编码风格中,“Case”将是一个简单的类名,而不是驼峰式的类或方法名。驼峰式命名法对于区分字符串中的单词非常有用。“Camelcase”也不是一个非常有用的标识符。从谷歌搜索中得出的结论是,Cidis的答案对我帮助最大。 - tm243

2

使用类似于inflection的库将您的字符串转换为驼峰式大小写。 如果它没有发生变化,那么它必须已经是驼峰式大小写。

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

0

我认为你可以通过检查字符串中是否有一个小写字母和一个大写字母来解决问题,如果(line =~ m/[a-z][A-Z]/)。仅检查大小写会在给定的示例中失败。 ~Ben


我已经修改了原始问题中的正则表达式。 - Dexter

0

这个正则表达式解决了我的问题:([A-Z][a-z\S]+)([A-Z][a-z]+)


0

驼峰命名法

是一种不使用空格或标点符号的写法, 用一个大写字母表示单词之间的分隔

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
  1. 搜索字符串中除字符以外的其他符号并存储在non_alpha
  2. 替换所有非字母符号为空格,并通过空格拆分它们以创建substrings
  3. 检查所有子字符串是否不完全大写,如果不是,则大写字母的索引不能为>0

输出

camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase False
Case False
camel_case False

0
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')

请问您能否添加一些关于您的代码片段如何以及为什么提供答案的解释?谢谢。 - deHaar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接