Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm not quite sure if this is the correct place to ask this question, but I'm working on a script that will identify the case of a string so upper case, lower case, and mixed case. I have upper and lower figured out but I'm ramming my head against the wall on mixed case.
If I input just lower case like "table" it will identify as lower and mixed
If I input just upper case like "TABLE" it will identify as upper and mixed
If I input mixed case like "Table" it will identify as mixed.
I guess my question is what would be a correct regular expression that only identifies a mixed case string like "TaBle" and not "table" and "TABLE"
Right now I'm using-
Uppercase- ^[[:upper:]]+$
Lowercase- ^[[:lower:]]+$
Mixed- ^[[:alpha:]]+$
Sorry for the long post, I'd appreciate any help!!
:alpha: is a superset containing both :upper: and :lower:, so the responses you’re getting are correct, technically.
Your logic needs to not test for mixed if either upper or lower are true.
It’s not a regular expression issue.
Edit: yes. We need to see the logic to be more specific
This is actually wrong. [:alpha:] means any sort of letter, it does not identify mixed case.
As the last 2 posts said, the logic you need is sth like this:
test if string is [:alpha:] and NOT [:upper:] and NOT [:lower:]
If the script is for the bash shell, then there is a useful parameter expansion
Code:
#!/bin/bash
v1="TABLE"
v2="table"
v3="Table"
for v in $v1 $v2 $v3; do
if [[ $v == ${v@U} ]]; then
echo "$v is Uppercase"
elif [[ $v == ${v@L} ]]; then
echo "$v is Lowercase"
else
echo "$v is Mixedcase"
fi
done
Sorry for the long post, I'd appreciate any help!!
Your post isn't long, but as mentioned you should include the actual function you currently have (rather than describing it), as well as clearly identifying what language[s] are involved - regular expressions syntax and functionality varies.
There was mention of "not upper and not lower" as a valid test for mixed case - in response I will point out that you will probably want to test how such a function responds if the input is an empty string.
Other inputs worth considering include:
Code:
A_
a_
12345
!"£$%
café straße
Good programming involves considering all possible inputs and behaving appropriately.
"All uppercase" and "All lowercase" are border cases of "All alpha" - need to be sorted out.
The following is like post #7 but spots the "Not alpha" case (including the "empty" case):
Code:
v1="TABLE"
v2="table"
v3="Table"
v4="A4"
v5=""
for v in "$v1" "$v2" "$v3" "$v4" "$v5"
do
if [[ $v =~ ^[[:upper:]]+$ ]]
then
echo "'$v' - Uppercase"
elif [[ $v =~ ^[[:lower:]]+$ ]]
then
echo "'$v' - Lowercase"
elif [[ $v =~ ^[[:alpha:]]+$ ]]
then
echo "'$v' - Mixed"
else
echo "'$v' - Not alpha"
fi
done
Last edited by MadeInGermany; 10-08-2021 at 03:02 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.