We help IT Professionals succeed at work.

Regular Expression using Java api not searching through string

sb1977
sb1977 asked
on
Medium Priority
254 Views
Last Modified: 2010-03-31
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);
}
Comment
Watch Question

>>>     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!
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>boolean doesNumberExist = numbers.matcher(password).matches();

should be

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

(matches90 has to match the *whole* String)

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
Typo in my answer:

matches90=matches()

Commented:
You want to just check if your string contains a digit right?

________
radarsh
Commented:
Well, this is more simplified:

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


________
radarsh

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I like the simplicity of radarsh's solution

How is it simpler than mine?! ;-)

Author

Commented:
I guess we commented at the same time, the last solution seems simple and should work with my other validations.  Thank you!
CERTIFIED EXPERT
Top Expert 2016

Commented:
Simplest of all would be

boolean doesNumberExist = password.find("\\d");

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
(It's also more efficient since it doesn't have any storage requirement)
CERTIFIED EXPERT
Top Expert 2016

Commented:
?

Author

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);
CERTIFIED EXPERT
Top Expert 2016

Commented:
Well since i didn't get credited for any of my previous efforts, you can't be asking for my advice presumably ..?

Commented:
>> 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

Author

Commented:
Sorry, I guess I accepted the answer too soon.  I'll create another one if that helps
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.