For an exercise I wrote an expression consisting of meta-characters which match at most 3 uppercase characters.
Example
a -> match
A -> match
Ab -> match
AbC -> match
AbCd -> match
...
ABCD -> no match, 4 uppercase chars
This is what I’ve come up with but I got a feeling I could make it shorter
ls @(!(*[A-Z]*)|*[A-Z]*|*[A-Z]*[A-Z]*|*[A-Z]*[A-Z]*[A-Z]*)
EDIT
Sry for the confusion. First of all, I’m only allowed to use meta-characters, no regular expressions, no test, no tools like awk/sed/something else.
Moreover, the uppercase letters must not be consecutive.
EDIT
Okay, this one seems to work (but is even longer!).
export LC_COLLATE=C
ls @(!(*[A-Z]*)|!(*[A-Z]*)[A-Z]!(*[A-Z]*)|[A-Z]!(*[A-Z]*)[A-Z]!(*[A-Z]*)|!(*[A-Z]*)[A-Z]!(*[A-Z]*)[A-Z]!(*[A-Z]*)[A-Z]!(*[A-Z]*)
Your pattern doesn’t work for me. One problem is that in many non-C locales
[A-Z]includes some lowercase characters.Try it again with
LANG=C. If you want to match only uppercase characters regardless of locale, use[[:upper:]].Another reason yours doesn’t work is that parts of it always match.
For example:
(even if it’s corrected using
[[:upper:]]) matches (rejects) anything that consists of only uppercase characters regardless of length. However, the rest of the (partially corrected) pattern includes uppercase characters explicitly while including any character including uppercase ones implicitly because of the asterisks. So just the first part of that:says to include all strings that consist of at least one uppercase character without regard to how many more there may be: one, ten, a million.
Instead, try this:
It simply checks to see if there are four or more uppercase characters.
You could also use a regular expression (in Bash 3.2 or greater):
Another way is to delete all the non-uppercase characters and compare the difference in lengths.
Demo: