Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:01:04+00:00 2026-05-20T02:01:04+00:00

For an exercise I wrote an expression consisting of meta-characters which match at most

  • 0

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]*)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:01:04+00:00Added an answer on May 20, 2026 at 2:01 am

    Your pattern doesn’t work for me. One problem is that in many non-C locales [A-Z] includes some lowercase characters.

    $ for c in a A b B z Z; do if [[ $c = [A-Z] ]]; then echo "match: $c"; else echo "no match: $c"; fi; done
    no match: a
    match: A
    match: b
    match: B
    match: z
    match: Z
    

    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:

    !(*[A-Z]*)
    

    (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:

    *[[:upper:]]*
    

    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:

    if [[ $string != *[[:upper:]]*[[:upper:]]*[[:upper:]]*[[:upper:]]* ]]
    then
        echo "match: fewer than four uppercase character"
    fi
    

    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):

    if [[ ! $string =~ ^.*[[:upper:]].*[[:upper:]].*[[:upper:]].*[[:upper:]].*$ ]]
    then
        echo "match: fewer than four uppercase character"
    fi
    

    Another way is to delete all the non-uppercase characters and compare the difference in lengths.

    Demo:

    #!/bin/bash
    strings[1]='AbCdEfghi'
    strings[2]='ABCD'
    strings[3]='Ab1Cd2Ef3ghi'
    strings[4]='A1BbC2Dd'
    
    for string in "${strings[@]}"
    do
        test=${string//[^[:upper:]]}
        if (( ${#test} > 3 ))
        then
            echo "no match: $string"
        else
            echo "match: $string"
        fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple MotionEvent exercise in which all I do is log the
I wrote simplest extension as an exercise in JS coding. This extension checks if
I wrote an in-place permutation algorithm [an exercise in TAOCP3], where the inner loop
There was a exercise in my book, which asked me to write a for
The exercise in this tutorial says: Generate a 10 x 3 array of random
I'm pretty new to lockless data structures, so for an exercise I wrote (What
This is taken from Exercise 19 of The Pragmatic Programmer. A quick reality check.
I'm solving this K&R exercise: Write versions of the library functions strncpy , strncat
I'm using a long regular expression which is pretty hard to grok if you
I'm stuck on a portion of the Java tutorial, specifically this exercise . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.