LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-07-2021, 07:48 PM   #1
mreazo
LQ Newbie
 
Registered: Oct 2021
Posts: 1

Rep: Reputation: Disabled
Regular expression


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!!
 
Old 10-07-2021, 10:34 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Please: "show us the actual code."

In whatever scripting language it may be.
 
Old 10-07-2021, 10:37 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
: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

Last edited by scasey; 10-07-2021 at 10:42 PM.
 
Old 10-07-2021, 11:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
For mixed the test must be not Uppercase AND not Lowercase.
 
Old 10-07-2021, 11:26 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Just to clarify what others already said:
Quote:
Originally Posted by mreazo View Post
Mixed- ^[[:alpha:]]+$
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:]
 
Old 10-08-2021, 01:47 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
You have to write a regexp which contains both upper and lowercase letters, but actually that is order dependent, so it will look like:
Code:
([[:upper:]][[:lower:]])|([[:lower:]][[:upper:]])
|           < is OR
()          < grouping
 
Old 10-08-2021, 02:07 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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
 
1 members found this post helpful.
Old 10-08-2021, 07:52 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,602

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by mreazo View Post
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.


Last edited by boughtonp; 10-08-2021 at 07:54 AM.
 
Old 10-08-2021, 02:17 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
"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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] jhalfs sed: -e expression #1, char 55:Invalid preceding regular expression percy_vere_uk Linux From Scratch 10 07-22-2017 07:15 AM
regular expression question Gantrep Linux - Software 2 04-20-2003 04:24 PM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM
regular expression for parsing html tags Bert Linux - Software 3 10-14-2002 04:31 PM
book recommendation for regular expression? doublefailure Programming 2 07-12-2002 12:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration