Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to insert some style tests as git hooks in order to stick with precise style rules. How can I come up with a oneliner that detects and prints arbitrary camelcase variables like the following:

camelCasehere
or
camelCaseHere
or
camel_caseHere
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

How about

/bin/grep -E '[a-z]{1,}[A-Z]'

This would require that at least one lowercase character is before an uppercase. This would match all your testcases.

If you want to also match something like camel_Case and not Nocamelcase you can use:

/bin/grep -E '([a-z]{1,}[A-Z])|(^.+[A-Z]{1,}[a-z])' 

To test it yourself you can use something like:

cat <<EOF | /bin/grep -E '([a-z]{1,}[A-Z])|(^.+[A-Z]{1,}[a-z])'
NONMATCH                                                               
nonmatch
shouldMatch1
should_Match2
should_mAtch3
Non_match    
Non_MATCH
Should_mAtch4
Should_Match5
EOF
share|improve this answer
    
The second solution is the best suited. –  ziu Jul 31 '12 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.