Regular Expression using Java api not searching through string

Hello - I need to do some server-side and client-side validations using regular expressions.  Below I have listed the client-side Javascript code which is working fine, but I can't seem to get the same results from my server-side Java code.  The regular expression only seems to return true when I have all numbers in my String, but I just want it to check for the presence of a number.

(Working Javascript code)

var doesSpecialCharExist = /[!@#$%^&*]/.exec(password);
var doesCodeCharExist  = /[<>’()='"]/.exec(password);
var doesNumberCharExist = /[0-9]/.exec(password);
var doesConsIdentCharExist = /(.)\1/.exec(password);
var doesUpperCaseCharExist = /[A-Z]/.exec(password);
var doesLowerCaseCharExist = /[a-z]/.exec(password);
var doesWhiteSpaceExist = /\s/.exec(password);

(Not working Java Code)

public static void validatePassword()
{
    String password = "asdfsf8";
    Pattern numbers = Pattern.compile("[0-9]");
    boolean doesNumberExist = numbers.matcher(password).matches();
    System.out.println(doesNumberExist);
}
sb1977Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
>>>     Pattern numbers = Pattern.compile("[0-9]");
That indeed checks for numbers of 1 digit.

As you want to parse the whole String, why not Integer.parse()?

;JOOP!
0
CEHJCommented:
>>boolean doesNumberExist = numbers.matcher(password).matches();

should be

boolean doesNumberExist = numbers.matcher(password).find();

(matches90 has to match the *whole* String)
0
radarshCommented:
Hi sb1977,

You just need to do this.

public static void validatePassword()
{
    String password = "asdfsf8";
    boolean doesNumberExist = password.matches("[a-z[^\\d]]*\\d+.*");
    System.out.println(doesNumberExist);
}

________
radarsh
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJCommented:
Typo in my answer:

matches90=matches()
0
radarshCommented:
You want to just check if your string contains a digit right?

________
radarsh
0
radarshCommented:
Well, this is more simplified:

public static void validatePassword()
{
    String password = "asdfsf8";
    boolean doesNumberExist = password.matches(".*[\\d].*");
    System.out.println(doesNumberExist);
}


________
radarsh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sb1977Author Commented:
My main question is to check for a number in the string, but I also have to get the other regular expressions working that I showed in the Javascript example.  I like the simplicity of radarsh's solution, but CEHJ's might work better for the expressions that I already have.
0
CEHJCommented:
>>I like the simplicity of radarsh's solution

How is it simpler than mine?! ;-)
0
sb1977Author Commented:
I guess we commented at the same time, the last solution seems simple and should work with my other validations.  Thank you!
0
CEHJCommented:
Simplest of all would be

boolean doesNumberExist = password.find("\\d");
0
radarshCommented:
Well, it's all simple. Just put .* at the beginning and end of your RegEx and it'll all work.

For example,

doesUpperCaseCharExist = string.matches(".*[A-Z].*");
doesWhiteSpaceExist = string.matches(".*[\\s].*");

And so on.

________
radarsh
0
CEHJCommented:
(It's also more efficient since it doesn't have any storage requirement)
0
CEHJCommented:
?
0
sb1977Author Commented:
The solution sounds really good, but what about looking for 2 consecutive, identical characters.  I can't seem that validation to work.  Here is what works in Javascript:

var doesConsIdentCharExist = /(.)\1/.exec(password);
0
CEHJCommented:
Well since i didn't get credited for any of my previous efforts, you can't be asking for my advice presumably ..?
0
radarshCommented:
>> It's also more efficient since it doesn't have any storage requirement

Well, efficient if you compile the pattern and save it as a static final field. I completely
agree on that.

However, you didn't change the RegEx... So, although efficient, it wouldn't have worked.


________
radarsh
0
sb1977Author Commented:
Sorry, I guess I accepted the answer too soon.  I'll create another one if that helps
0
CEHJCommented:
>>I'll create another one if that helps

Well it should be another one anyway (see site rules)

As for this one, it could be reopened if you wanted
0
sb1977Author Commented:
You're right, this led into another question and my first was answered in this forum, so I have created a new one specific to my new question.  Thank you both for your help so far.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.