7

文字列がキャメルケースかどうか(ブール値)を確認したいのですが。私は正規表現を使用する傾向がありますが、他のエレガントなソリューションならどれでも機能します。簡単な正規表現を書きました

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

これは正しいでしょうか?それとも私は何かが足りないのですか?

編集

この形式のテキストドキュメントのコレクションに名前をキャプチャしたい

McDowell
O'Connor
T.Kasting

Edit2

コメントの提案に基づいて正規表現を変更しました

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

8 に答える 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
于 2012-04-16T23:00:53.163 に答える
3

あなたはおそらくもっと次のようなものが欲しいでしょう:

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

すべてのキャップを許可するAltho。あなたはそれを避けることができます:

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

^必要に応じて、および$またはを使用して式をアンカーし\zます。

于 2012-04-16T22:39:44.320 に答える
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
于 2019-09-13T14:18:52.983 に答える
0

文字列の前に小文字が付いた大文字が含まれていることを確認するだけでうまくいくと思いますif(line =〜m / [az] [AZ] /)。与えられた例では、下限と上限をチェックするだけで失敗します。〜ベン

于 2012-04-16T23:02:48.050 に答える
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')
于 2019-07-30T06:20:36.520 に答える
0

大文字で始まる文字列を望まない場合、例えばCase、とCamelcaseを渡し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
于 2020-08-07T14:58:53.970 に答える
0

この正規表現ソリューションは私のユースケースで機能しました([A-Z][a-z\S]+)([A-Z][a-z]+)

于 2021-11-25T18:12:47.287 に答える
0

キャメルケース

スペースや句読点を使用せずにフレーズを書く方法であり 、 1つの大文字で単語を区切ることを示します

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. すべてのnon_alpha記号を空のスペースに置き換え、空のスペースで分割して作成しますsubstrings
  3. すべての部分文字列が完全に大文字ではないことを確認し、そうでない場合は大文字のインデックスを>0にすることはできません

出力

camel False
camelCase True
CamelCase True
CAMELCASE False
camelcase False
Camelcase False
Case False
camel_case False
于 2021-11-26T19:33:56.930 に答える