python - 在 Python 中检查驼峰大小写

标签 python regex camelcasing

我想检查一个字符串是否是驼峰式大小写( bool 值)。我倾向于使用正则表达式,但任何其他优雅的解决方案都可以。我写了一个简单的正则表达式

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

这是正确的吗?还是我遗漏了什么?

编辑

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

McDowell
O'Connor
T.Kasting

编辑2

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

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

最佳答案

您可以检查一个字符串是否同时具有大写和小写。

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

关于python - 在 Python 中检查驼峰大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10182664/

相关文章:

python - 将 ipython 设置为 pydev 的解释器

python - 如何在Django中使用重定向功能发送字典数据

正则表达式匹配条件匹配后的所有内容

ruby - 在 ruby​​ 中搜索字符串及其子字符串的数组

regex - 使用正则表达式,如何选择前 3 个单词(包括逗号/其他字符)?

c# - 代码分析,迷失在CA1709和CA1704之间

javascript - 将驼峰大小写字符串转换为烤肉串大小写的常规 rxpression

python - 如何解析python中的目录树?

c# - Json.NET 版本 4 中的 CamelCase 重大更改

python - Codefights : avoidObstacles, 函数内部多次返回