Regular Expression Alphanumeric String With Uppercase & Lowercase Characters

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.
speedygonzalezAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
sdstuberCommented:
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
 
TommySzalapskiCommented:
* allows 0 or more characters. Did you mean + instead? (one or more).
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
farzanjCommented:
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
 
speedygonzalezAuthor Commented:
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
 
speedygonzalezAuthor Commented:
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
 
farzanjCommented:
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
 
farzanjCommented:
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
 
TommySzalapskiConnect With a Mentor Commented:
What system are you using this on? This seems to work for me.
(?=.{8,8})(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*
0
 
speedygonzalezAuthor Commented:
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
 
user_nCommented:
For what exactly you need this regular expression(programing language, something else)? Be more specific.
0
 
speedygonzalezAuthor Commented:
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
 
farzanjCommented:
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
 
user_nCommented:
There are differences in Regular Expression in programming languages, so I think that it is important what language you are using.
0
 
farzanjConnect With a Mentor Commented:
This would work for you

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

Open in new window

0
 
TommySzalapskiCommented:
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
 
käµfm³d 👽Commented:
@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
 
farzanjCommented:
Hay, Kaufmed. Glad to see you here :)
0
 
speedygonzalezAuthor Commented:
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
 
speedygonzalezAuthor Commented:
Hi Kaufmed,  Thanks for your follow up.  I'll have a look into testing it out today.

Thanks again!
0
 
speedygonzalezAuthor Commented:
Hi all,  Thanks for your help.   I guess the question now is who should I give the points to?

Thanks again!
0
 
sdstuberCommented:
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
 
lhademmorCommented:
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
 
TommySzalapskiCommented:
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
 
lhademmorCommented:
Ups i did this to late at night i think. Sure You are right thanks :-).
0
 
speedygonzalezAuthor Commented:
Thanks all for the excellent help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.