tags:

views:

164

answers:

5

Hi

I have a string like this

>>> x="Alpha_beta_Gamma"
>>> words = [y for y in x.split('_')]
>>> words
['Alpha', 'beta', 'Gamma']

I want output saying X is non conformant as the the second element of the list words starts with a lower case and if the string x = "Alpha_Beta_Gamma" then it should print string is conformant

+2  A: 

You can use this regex:

^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$

Sample code:

import re

strings = ["Alpha_beta_Gamma", "Alpha_Beta_Gamma"]
pattern = r'^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$'

for s in strings:
    if re.match(pattern, s):
        print s + " conforms"
    else:
        print s + " doesn't conform"

As seen on codepad

NullUserException
+1 but it might fail for non-English characters, e.g. "Ălălalt".
Cristian Ciupitu
+5  A: 

To test that all words start with an upper case use this:

print all(word[0].isupper() for word in words)
Cristian Ciupitu
>>> x="Alpha_beta_Gamma">>> words = [y for y in x.split('_')]>>> print all(word[0].isupper() for word in words) File "<stdin>", line 1 print all(word[0].isupper() for word in words) ^SyntaxError: invalid syntax
lisa
@lisa: You have a really old Python version (<2.4). Write `print all([word[0].isupper() for word in words])` instead.
THC4k
@lisa: in Python3 use `print(all...)` because `print` is a function, not a statement.
Cristian Ciupitu
@THC4K: by the way old versions of Python didn't have `all()`. It is or was in a library provided by Google.
Cristian Ciupitu
@Cristian Ciupitu: Ah you are right, in Python3 it fails with a Syntax Error too. I just guessed the problem was the lack of generator expressions.
THC4k
Cristian well your answer by the way is brilliant to my original question
lisa
A: 
words = x.split("_")
for word in words:
    if word[0] == word[0].upper() and word[1:] == word[1:].lower():
        print word, "is conformant"
    else:
        print word, "is non conformant"
inspectorG4dget
A part of the code is inefficient. You can replace it with `word[0].isupper()`.
Cristian Ciupitu
And the other part with `word[1:].islower()`
NullUserException
Hi i am using python 3 and its giving me an error
lisa
@NullUserException: of course :-)@lisa: what error?
Cristian Ciupitu
@lisa: print is a function in Python 3. Please don't literally type the code here without *thinking* first and changing Python 2 things (like print statement) to Python 3 things like a print function. Also please don't say "giving me **an** error". Please provide the **specific** error.
S.Lott
@S.Lott Thanks for guiding me so whats the expected norm of posting a error here are the specific errors posted in the comment section
lisa
+11  A: 

Maybe you want str.istitle

>>> help(str.istitle)
Help on method_descriptor:

istitle(...)
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. uppercase characters may only follow uncased
    characters and lowercase characters only cased ones. Return False
    otherwise.

>>> "Alpha_beta_Gamma".istitle()
False
>>> "Alpha_Beta_Gamma".istitle()
True
>>> "Alpha_Beta_GAmma".istitle()
False
THC4k
+1 though there are a couple of titles that fail, e.g. `"ATM_for_dummies".istitle()` -> False.
Cristian Ciupitu
Well hmm i have to mark right all though all the answers here were correct based on the original requirement i had given.
lisa
Well hmm i have to mark your answer as right allthough all the answers here were correct based on the original requirement i had given.
lisa
A: 

You can use this code:

def is_valid(string):
    words = string.split('_')
    for word in words:
        if not word.istitle():
            return False, word
    return True, words
x="Alpha_beta_Gamma"
assert is_valid(x)==(False,'beta')
x="Alpha_Beta_Gamma"
assert is_valid(x)==(True,['Alpha', 'Beta', 'Gamma'])

This way you know if is valid and what word is wrong

Rafael SDM Sierra
Hi Rafael Thanks i appreciate your response
lisa