?
Solved

Regular Expression using Java api not searching through string

Posted on 2006-04-06
19
Medium Priority
?
221 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);
}
0
Comment
Question by:sb1977
  • 8
  • 5
  • 5
  • +1
19 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 16391427
>>>     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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391469
>>boolean doesNumberExist = numbers.matcher(password).matches();

should be

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

(matches90 has to match the *whole* String)
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16391490
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16391510
Typo in my answer:

matches90=matches()
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16391517
You want to just check if your string contains a digit right?

________
radarsh
0
 
LVL 10

Accepted Solution

by:
radarsh earned 600 total points
ID: 16391541
Well, this is more simplified:

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


________
radarsh
0
 

Author Comment

by:sb1977
ID: 16391543
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391563
>>I like the simplicity of radarsh's solution

How is it simpler than mine?! ;-)
0
 

Author Comment

by:sb1977
ID: 16391565
I guess we commented at the same time, the last solution seems simple and should work with my other validations.  Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391577
Simplest of all would be

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

Expert Comment

by:radarsh
ID: 16391584
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391591
(It's also more efficient since it doesn't have any storage requirement)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391604
?
0
 

Author Comment

by:sb1977
ID: 16391641
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391659
Well since i didn't get credited for any of my previous efforts, you can't be asking for my advice presumably ..?
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16391669
>> 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
 

Author Comment

by:sb1977
ID: 16391675
Sorry, I guess I accepted the answer too soon.  I'll create another one if that helps
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16391692
>>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
 

Author Comment

by:sb1977
ID: 16391721
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 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