?
Solved

Regular Expression Alphanumeric String With Uppercase & Lowercase Characters

Posted on 2012-08-14
26
Medium Priority
?
2,455 Views
Last Modified: 2012-08-29
Hello,   I have been trying to create an alphanumeric RegEX expression which contains capital and lowercase letters but have been struggling to get it to work.
After looking at the following sites http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html    http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/A_2140-Regular-Expressions-Starter-Guide.html
http://www.forcelabs.net/2011/07/regular-expression-to-validate-phone.html  I found the following

'(?!^[0-9]*$)(?!^[a-z]*$)^(?!^[A-Z]*$)^([a-zA-Z0-9]{8,8})$';

This I thought should check for alphanumeric with both capitals and smaller case letters but it does not.  it allows 'AA11111' for example to work. Also 'aa111111'.

Does anyone know how I can enforce the validation that both upper and lowercase characters are included in my string?

On a side note, For a separate check I could do with finding out if a string contains lowercase letters only (with no numbers).  Does anyone know the correct RegEx for this?

RegEx is confusing I have to say!!!!!!

Thanks in advance.
0
Comment
Question by:speedygonzalez
  • 8
  • 6
  • 4
  • +4
26 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 38292138
So you're looking for an expressions that ensures you have at least one capital letter and at least one lower case letter and it could also have other letters and also possibly digits?

 based on the {8,8} I'm guessing you want it to be exactly 8 total characters long?

is that correct?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38292236
* allows 0 or more characters. Did you mean + instead? (one or more).
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38292292
Here's regular expression for Alphanum
[a-z]+(?=.*[A-Z]+)(?=.*\d)

Open in new window


For Java or C++ that would be
[a-z]+(?=.*[A-Z]+)(?=.*\\d)

Open in new window


And for 8 digit alphanum with at least one char lower case, one upper case and one number is
(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*\d)(?=^[A-Za-z0-9]{8}$)

Open in new window


Again Java/C++ would be
(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*\\d)(?=^[A-Za-z0-9]{8}$)

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:speedygonzalez
ID: 38292415
Hi  sdstuber,  you are correct.  There may be more than one uppercase more than one lowercase, more than one digit.  There is at least 1 digit, 1 lowercase & 1 uppercase  & the password will be 8 characters in length.

TommySzalapski, thanks for your suggestion.  I will try this out.

farzanj,  I tried your java/C++   (?=.*[a-z]+)(?=.*[A-Z]+)(?=.*\\d)(?=^[A-Za-z0-9]{8}$)  and it always seems to return false.  I'm using Salesforce Apex which is based on Java.  I don't see a difference in your Java & non java versions.  is there a difference?

Thanks all!
0
 

Author Comment

by:speedygonzalez
ID: 38292530
Hi farzanj,  I see the difference in your java, non java code now (the escape characters).  I still can't get the options to work though at present.

Thanks again all for any help.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38292600
I had tested the regex using Perl.  So is it returning true for alphanum 8 characters long?

I assume you are using Java.  Which method are you using?
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38292645
Try this one with matches (it matches the entire string anyway).

(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*\\d)(?=[A-Za-z0-9]{8})

Open in new window

0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 668 total points
ID: 38292648
What system are you using this on? This seems to work for me.
(?=.{8,8})(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*
0
 

Author Comment

by:speedygonzalez
ID: 38292669
Hi Farzanj,  I'm using Salesforce.com's Apex language which is based on Java.  From this I am using the Pattern.matches function.   The following are being allowed for regex  
'(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*\\d)(?=^[A-Za-z0-9]{8}$)'

'Aa111111'
'AA111111'
'AAaaaa11'
'Az111111'
'aa111111'
'aaZZZZZZ'

The 2nd, 5th and 6th examples from above are incorrect.

Thanks again for your help.
0
 
LVL 9

Expert Comment

by:user_n
ID: 38292679
For what exactly you need this regular expression(programing language, something else)? Be more specific.
0
 

Author Comment

by:speedygonzalez
ID: 38292705
Hi user_n, it's for a password validation check.

TommySzalapski,  thanks for your help. Your RegEx is almost there.  It does seem to check for alphanumeric (with capital and lowercase letters) but does not restrict to 8 chars.  The following was allowed  'AAaaaa111111'

I guess if the 8 character restriction can be corrected it may work as I require.

Thanks.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38292719
Well, I would say it shouldn't.  I tried and it worked for me as expected.

(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*[0-9])(?=[A-Za-z0-9]{8})

Open in new window


If you had Regular expressions zone included with your question,  Kaufmed will be able to see and he or Ozo are the biggest Regex experts I know.
0
 
LVL 9

Expert Comment

by:user_n
ID: 38292728
There are differences in Regular Expression in programming languages, so I think that it is important what language you are using.
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 664 total points
ID: 38292732
This would work for you

(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=[A-Za-z0-9]{8})

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38292804
Why would you want to force an 8 character password anyway? Allowing longer passwords makes them more secure than forcing specific character sets. Also, forcing eight character passwords will make them harder for your users to remember (not as flexible) so they are more likely to forget or write it down somewhere (insecure).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38292811
@farzanj

What makes you think I wouldn't see the question  ;-P

SalesForce appears to use Java regex under the hood, so I believe farzanj's latest offering should work if it is bounded correctly. Tweaking his suggestion:

^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[A-Za-z0-9]{8}$

Open in new window


Also, I wrote an article discussing regex lookaround, and I mention password validation and provide an example. You may find Regular Expression Lookaround Demystified worth reading. (The password-related stuff is about half-way down under "Real World Examples".)

As for your question regarding, "if a string contains lowercase letters only (with no numbers)," that should simply be:

^[a-z]+$

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 38292863
Hay, Kaufmed. Glad to see you here :)
0
 

Author Comment

by:speedygonzalez
ID: 38295199
Hi All,  Thanks again for all your help.

Farzanj,  I'm not sure why it does not work for me but maybe its something to do with the Salesforce/Apex Class used for implementing this.  It's based on Java though therefore would have thought it should work if it does so on Java.

Hi Kaufmed,  Thank you for your help.   On trying out your solution it works correctly for most tests I ran except for 'aaZZZZZZ'  which it allowed to pass.

TommySzalapski's solution does work in that it seems to always match correctly to Alphanumeric which contain Uppercase & Lowercase characters.  I think to get over the 8 character restriction not working I can just check the length of the string separately.  The 8 character restriction is not by my design but instead something I have to implement.

Thanks all for your help.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 668 total points
ID: 38295608
Ah, I was going by the original post. I glossed over the later post that said you wanted to require numbers. You I left that out of my modification of farzanj's post; you can add it back:

^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])[A-Za-z0-9]{8}$

Open in new window

0
 

Author Comment

by:speedygonzalez
ID: 38299648
Hi Kaufmed,  Thanks for your follow up.  I'll have a look into testing it out today.

Thanks again!
0
 

Author Comment

by:speedygonzalez
ID: 38321564
Hi all,  Thanks for your help.   I guess the question now is who should I give the points to?

Thanks again!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38321573
whichever post or posts helped

if it's a tie then award to the one posted earlier

if they are nearly the same time then split between them
0
 

Expert Comment

by:lhademmor
ID: 38327421
I know re exs look nerd smart but as this question shows it can be very difficult to debug :-). Why not use a more understandeble way :-) ?

If it "locked" by 8 alphanums, why then not just run through with a for loop and test is char is Upper, Lower or number.
I don't know java, so sorry but a little like this :

int x, y;
x = 0;
for (y=0;y<8;y++)
{
  if (isUpper(password[y])) X++;
  if (isLower(password[y])) X+=2;
  if (isNumber(password[y])) X+=4;
}
If ( x != 7)
{
  printf("wrong");
  exit(-1);
}
printf("ok");
exit(0);
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38329507
Replace your ifs with
  if (isUpper(password[y])) x|=1;
  if (isLower(password[y])) x|=2;
  if (isNumber(password[y])) x|=4;

And it will work. Otherwise three uppers look like a lower and an upper etc.
0
 

Expert Comment

by:lhademmor
ID: 38330209
Ups i did this to late at night i think. Sure You are right thanks :-).
0
 

Author Closing Comment

by:speedygonzalez
ID: 38346394
Thanks all for the excellent help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question